ios 通讯录 通过电话号码 查询姓名

#pragma mark - 检索是否通讯录里有此号码


-(NSString *)getNameBytel:(NSString *)telstr

{

NSMutableArray* personArray = [[[NSMutableArray alloc] init] autorelease];

//打开电话本数据库

ABAddressBookRef addressRef=ABAddressBookCreate();

NSString *firstName, *lastName, *fullName;

//返回所有联系人到一个数组中

personArray = (NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressRef);

//返回联系人数量

// CFIndex personCount = ABAddressBookGetPersonCount(addressRef);

for (id person in personArray)

{

firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

firstName = [firstName stringByAppendingFormat:@" "];

lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

if (lastName !=nil)

{

fullName = [firstName stringByAppendingFormat:@"%@",lastName];


}

else

{

fullName = firstName;

}

NSLog(@"===%@",fullName);

ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonPhoneProperty);

for(int i = 0 ;i < ABMultiValueGetCount(phones); i++)

{

NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);

phone = [phone stringByReplacingOccurrencesOfString:@"(" withString:@""];

phone = [phone stringByReplacingOccurrencesOfString:@")" withString:@""];

phone = [phone stringByReplacingOccurrencesOfString:@"-" withString:@""];

phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""];

NSLog(@"===%@",phone);

if ([phone isEqualToString:telstr])

{

return fullName;

}

}

}

return nil;

}


你可能感兴趣的:(ios)