2013-06-13 40 views
0

我已经得到了每个人的姓名和电子邮件,我想根据这两个变量检索人的手机号码。我不想检索每个人的手机号码(以及姓名和电子邮件)。只是用户点击的那个。我怎样才能做到这一点?如何从地址簿中检索手机号码iOS

+2

[ABPerson参考](http://developer.apple.com/library/ios /#documentation/AddressBook/Reference/ABPersonRef_iPhoneOS/Reference/reference.html#//apple_ref/c/func/ABAddressBookCopyArrayOfAllPeople)&[地址簿编程指南iOS版](http://developer.apple.com/library/ios/ #documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Introduction.html) –

+0

我已经完成了所有这些。现在,我有每个人的姓名和电子邮件,我应该能够检索特定的人的手机号码(在哪个用户点击)。 –

+0

你可以。你还在等什么? – QED

回答

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