第一种,AddressBookUI获取这种是有UI效果的,但是这种一般只是获取通讯录点击某个联系人,也是目前最常用的。例如:微信、支付宝的话费获取通讯录
实现代码如下:
实现前提由于现在已经更新到iOS 11了,字10开始我们就要加一些权限问题,我们要获取手机通讯录,需要用户开一个权限,这个这两个形式必须都要添加
key:Privacy - Contacts Usage Description (必须) value:获取手机通讯录(随便)
首先,我们要导入一些官方提供的库,
#import
#import
#import
#import
#import
- (void)viewDidLoad {
[superviewDidLoad];
// 1. 创建联系人选择控制器
ABPeoplePickerNavigationController *pickVC = [[ABPeoplePickerNavigationControlleralloc]init];
// 2. 设置代理
pickVC.peoplePickerDelegate =self;
// 3. 弹出控制器
[selfpresentViewController:pickVCanimated:YEScompletion:nil];
}
// 选择某个联系人的时候调用
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {
NSLog(@"选中了某个联系人");
// 取出联系人的姓名数据
CFStringRef firstName =ABRecordCopyValue(person,kABPersonFirstNameProperty);
CFStringRef lastName =ABRecordCopyValue(person,kABPersonLastNameProperty);
// 将CoreFoundation的字符串, 转化为OC的字符串, 并且转移内存管理权
NSString *first =CFBridgingRelease(firstName);
NSString *last =CFBridgingRelease(lastName);
NSLog(@"%@--%@", first, last);
// 取出电话的数据
ABMultiValueRef multi =ABRecordCopyValue(person,kABPersonPhoneProperty);
CFIndex count =ABMultiValueGetCount(multi);
for (int i =0; i < count; i++) {
NSString *label = (__bridge_transferNSString *)ABMultiValueCopyLabelAtIndex(multi, i);
NSString *phone = (__bridge_transferNSString *)ABMultiValueCopyValueAtIndex(multi, i);
NSLog(@"%@---%@", label, phone);
}
}
这个是没有UI的,需要开发者自己搭建页面
代码如下:
- (void)viewDidLoad {
[superviewDidLoad];
[selfrequestAuthorizationAddressBook];
}
- (void)requestAuthorizationAddressBook {
// 判断是否授权
ABAuthorizationStatus authorizationStatus =ABAddressBookGetAuthorizationStatus();
if (authorizationStatus ==kABAuthorizationStatusNotDetermined) {
// 请求授权
ABAddressBookRef addressBookRef =ABAddressBookCreate();
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) { // 授权成功
} else { // 授权失败
NSLog(@"授权失败!");
}
});
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 1. 判读授权
ABAuthorizationStatus authorizationStatus =ABAddressBookGetAuthorizationStatus();
if (authorizationStatus !=kABAuthorizationStatusAuthorized) {
NSLog(@"没有授权");
return;
}
// 2. 获取所有联系人
ABAddressBookRef addressBookRef =ABAddressBookCreate();
CFArrayRef arrayRef =ABAddressBookCopyArrayOfAllPeople(addressBookRef);
long count =CFArrayGetCount(arrayRef);
NSLog(@"======================================================================================%d",count);
for (int i =0; i < count; i++) {
//获取联系人对象的引用
ABRecordRef people =CFArrayGetValueAtIndex(arrayRef, i);
//获取当前联系人名字
NSString *firstName=(__bridgeNSString *)(ABRecordCopyValue(people,kABPersonFirstNameProperty));
//获取当前联系人姓氏
NSString *lastName=(__bridgeNSString *)(ABRecordCopyValue(people,kABPersonLastNameProperty));
NSLog(@"--------------------------------------------------");
NSLog(@"firstName=%@, lastName=%@", firstName, lastName);
//获取当前联系人的电话 数组
NSMutableArray *phoneArray = [[NSMutableArrayalloc]init];
ABMultiValueRef phones =ABRecordCopyValue(people,kABPersonPhoneProperty);
for (NSInteger j=0; j<ABMultiValueGetCount(phones); j++) {
NSString *phone = (__bridgeNSString *)(ABMultiValueCopyValueAtIndex(phones, j));
NSLog(@"phone=%@", phone);
[phoneArray addObject:phone];
}
//获取当前联系人的邮箱 注意是数组
NSMutableArray *emailArray = [[NSMutableArrayalloc]init];
ABMultiValueRef emails=ABRecordCopyValue(people,kABPersonEmailProperty);
for (NSInteger j=0; j<ABMultiValueGetCount(emails); j++) {
NSString *email = (__bridgeNSString *)(ABMultiValueCopyValueAtIndex(emails, j));
NSLog(@"email=%@", email);
[emailArray addObject:email];
}
//获取当前联系人中间名
NSString *middleName=(__bridgeNSString*)(ABRecordCopyValue(people,kABPersonMiddleNameProperty));
NSLog(@"获取当前联系人中间名 ===============%@",middleName);
//获取当前联系人的名字前缀
NSString *prefix=(__bridgeNSString*)(ABRecordCopyValue(people,kABPersonPrefixProperty));
NSLog(@"获取当前联系人的名字前缀 ===============%@",prefix);
//获取当前联系人的名字后缀
NSString *suffix=(__bridgeNSString*)(ABRecordCopyValue(people,kABPersonSuffixProperty));
NSLog(@"获取当前联系人的名字后缀 ===============%@",suffix);
//获取当前联系人的昵称
NSString *nickName=(__bridgeNSString*)(ABRecordCopyValue(people,kABPersonNicknameProperty));
NSLog(@"获取当前联系人的昵称 ===============%@",nickName);
//获取当前联系人的名字拼音
NSString *firstNamePhoneic=(__bridgeNSString*)(ABRecordCopyValue(people,kABPersonFirstNamePhoneticProperty));
NSLog(@"获取当前联系人的名字拼音 ===============%@",firstNamePhoneic);
//获取当前联系人的姓氏拼音
NSString *lastNamePhoneic=(__bridgeNSString*)(ABRecordCopyValue(people,kABPersonLastNamePhoneticProperty));
NSLog(@"获取当前联系人的姓氏拼音 ===============%@",lastNamePhoneic);
//获取当前联系人的中间名拼音
NSString *middleNamePhoneic=(__bridgeNSString*)(ABRecordCopyValue(people,kABPersonMiddleNamePhoneticProperty));
NSLog(@"获取当前联系人的中间名拼音 ===============%@",middleNamePhoneic);
//获取当前联系人的公司
NSString *organization=(__bridgeNSString*)(ABRecordCopyValue(people,kABPersonOrganizationProperty));
NSLog(@"获取当前联系人的公司 ===============%@",organization);
//获取当前联系人的职位
NSString *job=(__bridgeNSString*)(ABRecordCopyValue(people,kABPersonJobTitleProperty));
NSLog(@"//获取当前联系人的职位 ===============%@",job);
//获取当前联系人的部门
NSString *department=(__bridgeNSString*)(ABRecordCopyValue(people,kABPersonDepartmentProperty));
NSLog(@"获取当前联系人的部门 ===============%@",department);
//获取当前联系人的生日
NSString *birthday=(__bridgeNSDate*)(ABRecordCopyValue(people,kABPersonBirthdayProperty));
NSLog(@"获取当前联系人的生日 ===============%@",birthday);
//获取当前联系人的备注
NSString *notes=(__bridgeNSString*)(ABRecordCopyValue(people,kABPersonNoteProperty));
NSLog(@"获取当前联系人的备注 ===============%@",notes);
//获取创建当前联系人的时间 注意是NSDate
NSDate *creatTime=(__bridgeNSDate*)(ABRecordCopyValue(people,kABPersonCreationDateProperty));
NSLog(@"获取创建当前联系人的时间 注意是NSDate ===============%@",creatTime);
//获取最近修改当前联系人的时间
NSDate *alterTime=(__bridgeNSDate*)(ABRecordCopyValue(people,kABPersonModificationDateProperty));
NSLog(@"获取最近修改当前联系人的时间 ===============%@",alterTime);
NSLog(@"------------------------------------------------------------------------------------------------------------------------------");
//获取地址
ABMultiValueRef address =ABRecordCopyValue(people,kABPersonAddressProperty);
for (int j=0; j<ABMultiValueGetCount(address); j++) {
//地址类型
NSString *type = (__bridgeNSString *)(ABMultiValueCopyLabelAtIndex(address, j));
NSDictionary * tempDic = (__bridgeNSDictionary *)(ABMultiValueCopyValueAtIndex(address, j));
//地址字符串,可以按需求格式化
NSString *adress = [NSStringstringWithFormat:@"国家:%@\n省:%@\n市:%@\n街道:%@\n邮编:%@",[tempDic valueForKey:(NSString*)kABPersonAddressCountryKey],[tempDic valueForKey:(NSString*)kABPersonAddressStateKey],[tempDic valueForKey:(NSString*)kABPersonAddressCityKey],[tempDic valueForKey:(NSString*)kABPersonAddressStreetKey],[tempDic valueForKey:(NSString*)kABPersonAddressZIPKey]];
}
//获取当前联系人头像图片
NSData *userImage=(__bridgeNSData*)(ABPersonCopyImageData(people));
//获取当前联系人纪念日
NSMutableArray *dateArr = [[NSMutableArrayalloc]init];
ABMultiValueRef dates=ABRecordCopyValue(people,kABPersonDateProperty);
for (NSInteger j=0; j<ABMultiValueGetCount(dates); j++) {
//获取纪念日日期
NSDate *data =(__bridgeNSDate*)(ABMultiValueCopyValueAtIndex(dates, j));
//获取纪念日名称
NSString *str =(__bridgeNSString*)(ABMultiValueCopyLabelAtIndex(dates, j));
NSDictionary *tempDic = [NSDictionarydictionaryWithObject:dataforKey:str];
[dateArr addObject:tempDic];
}
}
}