首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

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

2012-06-20 
ios 通讯录 通过电话号码 查询姓名#pragma mark - 检索是否通讯录里有此号码-(NSString *)getNameBytel:(N

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

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


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

{

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

//打开电话本数据库

    ABAddressBookRef addressRef=ABAddressBookCreate();

      NSString *firstName, *lastName, *fullName; 

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

    personArray = (NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressRef); 

    

    //返回联系人数量

//    CFIndex personCount = ABAddressBookGetPersonCount(addressRef);

    

for (id personin 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 = [phonestringByReplacingOccurrencesOfString:@"("withString:@""];

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

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

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

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

if ([phoneisEqualToString:telstr]) 

{

return fullName;

}

}

return nil;

}


热点排行