如何获取与某个电话号码相关的联系人姓名?

时间:2013-11-17 17:15:58

标签: ios iphone addressbook phone-number

在我的应用程序中,我收到服务器的json响应,其中包含电话号码。如果号码存储在地址簿中,我不想显示联系人的全名。与手机应用程序类似,当您手动输入数字时,如果数字存储在地址簿中,iPhone会显示名称。

我在网上找不到任何教程。可能由于搜索字符串总是包含iphone,数字和联系人而且我得到了很多结果但没有任何帮助。

任何人都可以给我一个提示,看看或怎么做?

1 个答案:

答案 0 :(得分:0)

在此处获得解决方案

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