美文网首页iOS 开发 学习
ContactsUI获取通讯录联系人信息 ContactsUI

ContactsUI获取通讯录联系人信息 ContactsUI

作者: 清蘂翅膀的技术 | 来源:发表于2017-05-16 13:57 被阅读65次

第1步:准备工作

导入头文件#import

遵守协议

第2步: 显示选择联系人控制器

//获取通讯录方法

- (void) getContactInfo

{

//创建选择联系人的导航控制器

peoplePickVC = [[CNContactPickerViewControlleralloc] init];

//设置代理

peoplePickVC.delegate =self;

//弹出联系人界面

[selfshowViewController:peoplePickVC sender:nil];

}

注意:如果Xcode提示:plugin com.apple.MobileAddressBook.ContactsViewService invalidated,只要把CNContactPickerViewController设置为全局变量即可:`

staticCNContactPickerViewController* peoplePickVC;

第3步:实现代理方法

如果同时实现了选中多个和单个联系人的代理方法,那么即便选单个联系人,也是会走选中多个联系人的代理方法(只不过数组只保留一个联系人),单人选中方法被忽略

单个联系人选中方法和选中联系人号码属性方法不可以同时实现,因为既然选中了单个联系人,就没办法进入联系人详情界面再去选中号码属性了

代理方法1:选中单个联系人时调用这个方法

getSingleContact.gif

-(void)contactPicker:(CNContactPickerViewController*)picker didSelectContact:(CNContact*)contact

{

//获取联系人姓名

NSString* firstName = contact.familyName;

NSString* lastName = contact.givenName;

NSLog(@"姓名:%@ %@",firstName,lastName);

//数组保存各种类型的联系方式的字典(可以理解为字典) 字典的key和value分别对应号码类型和号码

NSArray* phoneNums = contact.phoneNumbers;

//通过遍历获取联系人各种类型的联系方式

for(CNLabeledValue*labelValueinphoneNums)

{

//取出每一个字典,根据键值对取出号码和号码对应的类型

NSString*phoneValue = [labelValue.value stringValue];

NSString*phoneLabel = labelValue.label;

NSLog(@"%@:%@",phoneLabel,phoneValue);

}

}

代理方法2:选中联系人的单个号码类型时调用这个方法

getSingleContactProperty.gif

-(void)contactPicker:(CNContactPickerViewController*)picker didSelectContactProperty:(CNContactProperty*)contactProperty

{

//获取联系人信息

NSString* firstName = contactProperty.contact.familyName;//姓

NSString* lastName = contactProperty.contact.givenName;//名

NSString* phoneNum = [contactProperty.value stringValue];//号码

NSString* phoneLabel = contactProperty.label;//号码类型

NSLog(@"姓名:%@ %@ \n %@:%@",firstName,lastName, phoneLabel, phoneNum);

}

代理方法3:选中多个联系人时调用这个方法

getMultiContact.gif

-(void)contactPicker:(CNContactPickerViewController*)picker didSelectContacts:(NSArray *)contacts

{

//遍历选中联系人数组,获取联系人姓名和电话

for(CNContact* contactincontacts)

{

//获取联系人姓名

NSString* firstName = contact.familyName;

NSString* lastName = contact.givenName;

NSLog(@"姓名:%@ %@",firstName,lastName);

//数组保存各种类型的联系方式的字典(可以理解为字典) 字典的key和value分别对应号码类型和号码

NSArray* phoneNums = contact.phoneNumbers;

//通过遍历获取联系人各种类型的联系方式

for(CNLabeledValue*labelValueinphoneNums)

{

//取出每一个字典,根据键值对取出号码和号码对应的类型

NSString*phoneValue = [labelValue.value stringValue];

NSString*phoneLabel = labelValue.label;

NSLog(@"%@:%@",phoneLabel,phoneValue);

}

}

}

代理方法4:选中联系人的多个号码类型时调用这个方法

-(void)contactPicker:(CNContactPickerViewController*)picker didSelectContactProperties:(NSArray *)contactProperties

{

//取出第一个属性,获取联系人姓名

CNContactProperty* contactProperty = contactProperties.firstObject;

NSString* firstName = contactProperty.contact.familyName;//姓

NSString* lastName = contactProperty.contact.givenName;//名

NSLog(@"姓名:%@ %@ ",firstName,lastName);

//遍历选中多个类型号码数组的详细信息

for(CNContactProperty* contactPropertyincontactProperties)

{

NSString* phoneNum = [contactProperty.value stringValue];//号码

NSString* phoneLabel = contactProperty.label;//号码类型

NSLog(@"%@:%@",phoneLabel, phoneNum);

}

}

注意:只实现该方法会停留在选择多个联系人界面,要对控制器的predicateForSelectionOfProperty属性设置要筛选的条件,只有满足筛选条件的联系人才可以被选中。这个怎么设置我实在编不下去了😜😜😜,好像是正则表达式的内容,也没找到相关资料,希望有大神看到不吝赐教!

peoplePickVC.predicateForSelectionOfProperty = [NSPredicate predicateWithFormat:@"(key =='emailAddresses') AND (value LIKE'*@mac.com')"];

代理方法5:点击取消选择联系人(或联系人属性时调用这个方法)

cancelSelect.gif

-(void)contactPickerDidCancel:(CNContactPickerViewController *)picker

{

NSLog(@"取消选择联系人!");

}

详细写法:

#import "ViewController.h"

#import

@interfaceViewController ()

{

UIButton * btn;

}

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

btn = [UIButton buttonWithType:UIButtonTypeCustom];

[btn setTitle:@"通讯录"forState:UIControlStateNormal];

btn.frame = CGRectMake(0, 100, 200, 200);

btn.backgroundColor = [UIColor cyanColor];

[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

[btn addTarget:selfaction:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

-(void)pressBtn{

//让用户给权限,没有的话会被拒的各位

CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];

if(status == CNAuthorizationStatusNotDetermined) {

CNContactStore *store = [[CNContactStore alloc] init];

[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOLgranted,NSError* _Nullable error) {

if(error) {

NSLog(@"weishouquan ");

}else

{

NSLog(@"chenggong ");//用户给权限了

CNContactPickerViewController * picker = [CNContactPickerViewControllernew];

picker.delegate =self;

picker.displayedPropertyKeys = @[CNContactPhoneNumbersKey];//只显示手机号

[selfpresentViewController: picker  animated:YEScompletion:nil];

}

}];

}

if(status == CNAuthorizationStatusAuthorized) {//有权限时

CNContactPickerViewController * picker = [CNContactPickerViewControllernew];

picker.delegate =self;

picker.displayedPropertyKeys = @[CNContactPhoneNumbersKey];

[selfpresentViewController: picker  animated:YEScompletion:nil];

}

else{

NSLog(@"您未开启通讯录权限,请前往设置中心开启");

}

}

-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{

CNPhoneNumber * num =nil;

NSString* string =nil;

if(contact.phoneNumbers.count >0) {

num = contact.phoneNumbers[0].value;

string = [NSStringstringWithFormat:@"%@%@%@",contact.familyName,contact.givenName,[num valueForKey:@"digits"]];

}else{

string = [NSStringstringWithFormat:@"%@%@",contact.familyName,contact.givenName];

}

NSLog(@"%@",string);//2016-12-12 10:28:06.823028 通讯录[17274:5041432] 滴滴出行4001809660

[btn setTitle:string forState:UIControlStateNormal];

}

相关文章

网友评论

    本文标题:ContactsUI获取通讯录联系人信息 ContactsUI

    本文链接:https://www.haomeiwen.com/subject/badlxxtx.html