我需要能够从用户的地址簿中读取联系人的电话号码。问题是,如果用户选择通过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用户&amp;得到他们的电话信息?
答案 0 :(得分:2)
来自Apple支持:
我们没有任何可以返回统一通讯簿的API 联系。但是,您可以检索该联系人的电话号码 首先使用,在您设备的“通讯录”中显示为统一 ABPersonCopyArrayOfAllLinkedPeople检索所有链接的联系人, 然后迭代这些联系人来获取他们各自的手机 数字。请注意,UI中未显示的电话号码 通讯簿API不会返回联系人。见下文 允许您这样做的代码片段:
- (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;