标签:
一、创建通讯录对象
self.addressBook=ABAddressBookCreateWithOptions(NULL, NULL);
//请求访问用户通讯录,注意无论成功与否block都会调用
ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error) {
if (!granted) {
NSLog(@"未获得通讯录访问权限!");
}
});
ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;
[self presentViewController:peoplePicker animated:YES completion:nil];
二、实现代理协议
ABPeoplePickerNavigationControllerDelegate
三、实现代理方法
#pragma mark -- ABPeoplePickerNavigationControllerDelegate
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
//取得记录中得信息
ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
//lastName为姓,firstName为名,将两个字符串连接起来为wholeName全名
NSString *firstName=(__bridge NSString *) ABRecordCopyValue(person, kABPersonFirstNameProperty);//注意这里进行了强转,不用自己释放资源
NSString *lastName=(__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *wholeName=@"";
if (firstName.length>0) {
if (lastName.length>0) {
wholeName=[lastName stringByAppendingString:firstName];
}
else{
wholeName=firstName;
}
}
else {
if (lastName.length>0)
{
wholeName=[wholeName stringByAppendingString:lastName];
}
else{
wholeName=@"未知联系人";
}
}
CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef,identifier);
NSString *value = (__bridge NSString *)ABMultiValueCopyValueAtIndex(valuesRef,index);
[self dismissViewControllerAnimated:YES completion:^{
if(value.length!=0||wholeName.length!=0)
{
//一般读取过来的号码会是短线连接格式,下面方法可以去掉号码横杠
NSArray *NBArr=[value componentsSeparatedByString:@"-"];
NSString *finalyNB=@"";
for (int i=0; i<NBArr.count; i++) {
finalyNB=[finalyNB stringByAppendingString:[NBArr objectAtIndex:i]];
}
NSLog(@"%@,%@",wholeName,finalyNB);
}
}];
}
iOS读取通讯录获取好友通讯录信息[名字(姓+名字),手机号码(多个号码)等]
标签:
原文地址:http://www.cnblogs.com/pengStyle/p/5013405.html