如何从iPhone联系人中检索手机号码。

时间:2010-08-23 10:00:00

标签: iphone objective-c contacts

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

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以及如何使用?好像我将它替换为上面的代码,它有错误。谁能帮我?谢谢。

4 个答案:

答案 0 :(得分:16)

检查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);
    }

答案 1 :(得分:5)

地址簿中某人的电话号码采用多值属性的形式。

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

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;
    }
}

答案 2 :(得分:2)

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;
    }
}

答案 3 :(得分:0)

这对ARC 64bit 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;
}