【问题标题】:How to retrieve mobile numbers from iPhone Contacts.如何从 iPhone 联系人中检索手机号码。
【发布时间】:2010-08-23 10:00:00
【问题描述】:

我使用以下代码在我的应用中设置检索电话号码。

CFStringRef addressBookMobile;
ABRecordRef person;
NSString *mobile;

person = CFArrayGetValueAtIndex(people, i);
addressBookMobile = ABRecordCopyValue(person, kABPersonPhoneProperty);
mobile = [NSString stringWithFormat:@"%@", addressBookMobile];

联系人的标签是“移动”。但是,当我使用 NSLog(@"%@", mobile); 时。它显示<NSCFType: 0x802ffc0>。我的代码有什么问题吗?

我应该使用const CFStringRef kABPersonPhoneMobileLabel 以及如何使用?好像我将它替换为上面的代码一样,它有错误。谁能帮我?谢谢。

【问题讨论】:

    标签: iphone objective-c contacts


    【解决方案1】:

    检查 ABPerson Refrence 并且您不需要使用 @"$!!$" 而是 kABPersonPhoneMobileLabel。例子是:

        ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
        NSString* mobile=@"";
        NSString* mobileLabel;
        for (int i=0; i < ABMultiValueGetCount(phones); i++) {
            //NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
            //NSLog(@"%@", phone);
            mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
            if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) {
                NSLog(@"mobile:");
            } else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) {
                NSLog(@"iphone:");
            } else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhonePagerLabel]) {
                NSLog(@"pager:");
            }
            [mobile release];
            mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"%@", mobile);
        }
    

    【讨论】:

    • 为什么它在这一行显示错误 ABMultiValueRef 电话 = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty); NSString* 移动=@"";未申报的人说。我正在使用 ios 5
    【解决方案2】:

    通讯录中一个人的电话号码是多值属性的形式。

    在你的情况下,你应该有类似以下的东西(没有尝试过,直接在这里输入,所以我不知道它是否编译和/或工作):

    ABMultiValueRef phoneNumbers = (NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString *mobileNumber;
    NSString *mobileLabel;
    for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
        mobileLabel = (NSString *)ABMultiValueCopyLabelAtIndex(mobilePhones, i);
        if ([mobileLabel isEqualToString:@"mobile"]) {
            mobileNumber = (NSString*)ABMultiValueCopyValueAtIndex(mobilePhones,i);
            break;
        }
    }
    

    【讨论】:

    • lancu,谢谢您的回复。但我发现解决方案可能不正确。结果是一样的,我想问一下,代码中的手机是什么?谢谢。
    • 对不起,马克,它应该是 phoneNumbers :-)。正如我所说,我直接在表单中输入它,而不是在 Xcode 中:-)。所以不用mobilePhones,换成phoneNumbers,应该没问题。
    【解决方案3】:
    ABMultiValueRef phoneNumbers = (NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty);
    CRStringRef mobileNumber;
    CRStringRef mobileLabel;
    for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
        mobileLabel = ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
        if ([mobileLabel isEqualToString:@"_$!<Mobile>!$_"]) {
            mobileNumber = ABMultiValueCopyValueAtIndex(phoneNumbers,i);
            break;
        }
    }
    

    【讨论】:

      【解决方案4】:

      这对于 ARC 64 位 iOS8 来说是可靠的:

      - (NSArray *)phoneNumbersOfContactAsStrings:(ABRecordRef)contactRef {
      
      NSMutableArray *mobilePhones = [NSMutableArray arrayWithCapacity:0];
      
      ABMultiValueRef phones = ABRecordCopyValue(contactRef, kABPersonPhoneProperty);
      NSArray *allPhoneNumbers = (NSArray *)CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phones));
      
      for (NSUInteger i=0; i < [allPhoneNumbers count]; i++) {
          if ([(NSString *)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, (long)i)) isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) {
              [mobilePhones addObject:CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, (long)i))];
          }
          if ([(NSString *)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, (long)i)) isEqualToString:(NSString *)kABPersonPhoneIPhoneLabel]) {
              [mobilePhones addObject:CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, (long)i))];
          }
      }
      
      CFRelease(phones);
      return mobilePhones;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 2011-10-30
        • 2016-09-26
        • 1970-01-01
        • 2016-12-15
        • 2012-05-28
        相关资源
        最近更新 更多