如何从地址簿iOS中检索手机号码

时间:2013-06-13 05:29:08

标签: ios search addressbook

我有每个人的姓名和电子邮件,我想根据这两个变量检索人的手机号码。我不想检索每个人的手机号码(以及姓名和电子邮件)。仅适用于用户点击的那个。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

使用此代码根据名字和电子邮件ID ....

获取特定联系人手机号码
NSMutableString *contactNumber = [[NSMutableString alloc] init];

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for(int i=0; i<nPeople; i++)
{
    ABRecordRef person=CFArrayGetValueAtIndex(people, i);

    NSMutableString *contacName = [[NSMutableString alloc] init];
    [contacName stringByAppendingString:@""];
    [contacName appendFormat:@"%@", (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty)];
    ABMultiValueRef emailInfo = ABRecordCopyValue(person, kABPersonEmailProperty);
    CFIndex emailCount = ABMultiValueGetCount(emailInfo);
    NSString *emailId;
    if(emailCount > 0)
    {
        emailId = (NSString *)ABMultiValueCopyValueAtIndex(emailInfo, 0);
    }

    if(([contacName isEqualToString:@"<<contatct name>>"]) && ([emailId isEqualToString:@"<<email id>>"]))
    {
        [contactNumber appendFormat:@"%@", (NSString *)ABRecordCopyValue(person, kABPersonPhoneMobileLabel)];
        break;
    }
}
CFRelease(people);
CFRelease(addressBook);


NSLog("contact Mobile number %@", contactNumber);