【问题标题】:How to get the name of a contact related to a certain phone number?如何获取与某个电话号码相关的联系人姓名?
【发布时间】:2013-11-17 17:15:58
【问题描述】:
在我的应用程序中,我收到来自服务器的 json 响应,其中包含电话号码。如果号码存储在通讯录中,我不想显示联系人的全名。与手动输入号码时的手机应用程序类似,如果号码存储在地址簿中,iPhone 会显示姓名。
我在网上找不到任何教程。可能是由于搜索字符串总是包含 iphone、号码和联系人,我得到了很多结果但没有任何帮助。
谁能告诉我在哪里看或如何做?
【问题讨论】:
标签:
ios
iphone
addressbook
phone-number
【解决方案1】:
在这里获得解决方案
How to search the iphone address book for a specific phone number?
- (void) scanAddressBookSample
{
NSUInteger i;
NSUInteger k;
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *people = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);
if ( people==nil )
{
NSLog(@"NO ADDRESS BOOK ENTRIES TO SCAN");
CFRelease(addressBook);
return;
}
for ( i=0; i<[people count]; i++ )
{
ABRecordRef person = (ABRecordRef)[people objectAtIndex:i];
//
// Phone Numbers
//
ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex phoneNumberCount = ABMultiValueGetCount( phoneNumbers );
for ( k=0; k<phoneNumberCount; k++ )
{
CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex( phoneNumbers, k );
CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNumbers, k );
CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel( phoneNumberLabel ); // converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile"
// Find the ones you want here
//
NSLog(@"-----PHONE ENTRY -> %@ : %@", phoneNumberLocalizedLabel, phoneNumberValue );
CFRelease(phoneNumberLocalizedLabel);
CFRelease(phoneNumberLabel);
CFRelease(phoneNumberValue);
}
}
[people release];
CFRelease(addressBook);
}