iOS-获取通讯录联系人信息
头文件
#import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h>
授权
关于通讯录的授权这里不再多说了,我在上一篇中有提到:通讯录授权
访问通讯录
跳转到系统通讯录
- (void)jumpAddress{ ABPeoplePickerNavigationController *peoplePickVc = [[ABPeoplePickerNavigationController alloc] init]; peoplePickVc.peoplePickerDelegate = self; [self presentViewController:peoplePickVc animated:YES completion:nil]; }
添加代理
@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate, UINavigationControllerDelegate>
代理方法
///取消选择 - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{ }
///实现此方法后,下面的代理方法不会实现(此方法是点击通讯录列表中名字直接调用并dismiss) - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person { //获取当前联系人名字 NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); //获取当前联系人姓氏 NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)); //获取当前联系人的电话 数组 NSMutableArray *phoneArray = [[NSMutableArray alloc]init]; ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty); for (NSInteger j=0; j<ABMultiValueGetCount(phones); j++) { NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j)); NSLog(@"phone=%@", phone); [phoneArray addObject:phone]; } //获取当前联系人的邮箱 注意是数组 NSMutableArray *emailArray = [[NSMutableArray alloc]init]; ABMultiValueRef emails= ABRecordCopyValue(person, kABPersonEmailProperty); for (NSInteger j=0; j<ABMultiValueGetCount(emails); j++) { NSString *email = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(emails, j)); NSLog(@"email=%@", email); [emailArray addObject:email]; } //获取当前联系人中间名 NSString *middleName=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonMiddleNameProperty)); //获取当前联系人的名字前缀 NSString *prefix=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonPrefixProperty)); //获取当前联系人的名字后缀 NSString *suffix=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonSuffixProperty)); //获取当前联系人的昵称 NSString *nickName=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonNicknameProperty)); //获取当前联系人的名字拼音 NSString *firstNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty)); //获取当前联系人的姓氏拼音 NSString *lastNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty)); //获取当前联系人的中间名拼音 NSString *middleNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty)); //获取当前联系人的公司 NSString *organization=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonOrganizationProperty)); //获取当前联系人的职位 NSString *job=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonJobTitleProperty)); //获取当前联系人的部门 NSString *department=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonDepartmentProperty)); //获取当前联系人的生日 // NSString *birthday=(__bridge NSDate*)(ABRecordCopyValue(person, kABPersonBirthdayProperty)); //获取当前联系人的备注 NSString *notes=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonNoteProperty)); //获取创建当前联系人的时间 注意是NSDate NSDate *creatTime=(__bridge NSDate*)(ABRecordCopyValue(person, kABPersonCreationDateProperty)); //获取最近修改当前联系人的时间 NSDate *alterTime=(__bridge NSDate*)(ABRecordCopyValue(person, kABPersonModificationDateProperty)); //获取当前联系人头像图片 NSData *userImage=(__bridge NSData*)(ABPersonCopyImageData(person)); //获取当前联系人纪念日 NSMutableArray *dateArr = [[NSMutableArray alloc]init]; ABMultiValueRef dates= ABRecordCopyValue(person, kABPersonDateProperty); for (NSInteger j=0; j<ABMultiValueGetCount(dates); j++) { //获取纪念日日期 NSDate *data =(__bridge NSDate*)(ABMultiValueCopyValueAtIndex(dates, j)); //获取纪念日名称 NSString *str =(__bridge NSString*)(ABMultiValueCopyLabelAtIndex(dates, j)); } }
///实现此方法后,上面的代理方法不会实现(此方法是进入通讯录详情后点击名称或电话等调用并dismiss) - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0){ }
注:上面两个选择回调代理实现一个就可以
相关借鉴:http://www.jb51.net/article/95500.htm