4

我需要能够从用户的通讯录中读取联系人的电话号码。问题是,如果用户选择通过 Facebook 同步这些联系人,则无法再通过以下代码访问它们(这对未同步的联系人有效):

ABMultiValueRef phones = ABRecordCopyValue(record, kABPersonPhoneProperty);
DLog(@"Found %ld phones", ABMultiValueGetCount(phones));
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
{        
    CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
    CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, j);
    NSString *phoneLabel =(__bridge NSString*) ABAddressBookCopyLocalizedLabel(locLabel);
    NSString *phoneNumber = (__bridge NSString *)phoneNumberRef;
    CFRelease(phoneNumberRef);
    CFRelease(locLabel);
    DLog(@"  - %@ (%@)", phoneNumber, phoneLabel);
    [numbersArr addObject:phoneNumber];
}

日志结果是[Line 126] Found 0 phones

我曾尝试使用CFArrayRef userNumbers = ABMultiValueCopyArrayOfAllValues(phoneNumbers);
,但这也没有返回任何内容:[Line 118] Got user numbers: (null)

所以我试图深入研究社交资料,这也没有任何回报!

// Try to get phone numbers from social profile
        ABMultiValueRef profiles = ABRecordCopyValue(record, kABPersonSocialProfileProperty);
        CFIndex multiCount = ABMultiValueGetCount(profiles);
        for (CFIndex i=0; i<multiCount; i++) {
            NSDictionary* profile = (__bridge NSDictionary*)ABMultiValueCopyValueAtIndex(profiles, i);
            NSLog(@"TESTING - Profile: %@", profile);

        }
        DLog(@"Got profiles: %@", profiles);
        CFRelease(profiles);

然而日志条目是: [Line 161] Got profiles: ABMultiValueRef 0x1ddbb5c0 with 0 value(s)

如果上述结果都没有给我任何帮助,我怎么知道他们是 Facebook 用户并获取他们的电话信息?

4

1 回答 1

2

来自 Apple 支持:

我们没有任何 API 可以返回统一的通讯簿联系人。但是,您可以通过首先使用 ABPersonCopyArrayOfAllLinkedPeople 检索所有链接的联系人,然后遍历这些联系人以获取其各自的电话号码,来检索在您设备上的联系人中统一显示的联系人的电话号码。请注意,通讯录 API 不会返回未出现在联系人 UI 中的电话号码。请参阅下面的代码片段,您可以这样做:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person

{

//Fetch all linked contacts

CFArrayRef linkedPerson = ABPersonCopyArrayOfAllLinkedPeople(person);

//Iterate through each linked contact to fetch the phone numbers

for (CFIndex i = 0; i < CFArrayGetCount(linkedPerson); i++)

{

ABRecordRef contact = CFArrayGetValueAtIndex(linkedPerson,i);

ABMutableMultiValueRef multi = ABRecordCopyValue(contact, kABPersonPhoneProperty);

for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++)

{

CFStringRef label = ABMultiValueCopyLabelAtIndex(multi, i);

CFStringRef number = ABMultiValueCopyValueAtIndex(multi, i);

CFRelease(label);

CFRelease(number);

}

CFRelease(multi);

}

CFRelease(linkedPerson);

return YES;
于 2013-03-25T18:21:08.497 回答