iOS9 通讯录 CNContact
ContactsUI:系统通讯录视图,选择联系人
//弹出联系人列表
CNContactPickerViewController * con = [[CNContactPickerViewController alloc]init];
[self presentViewController:con animated:YES completion:nil];
实现代理:读取联系人信息
//视图取消时 调用的方法
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker;
//选中与取消选中时调用的方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact*> *)contacts;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperties:(NSArray<CNContactProperty*> *)contactProperties;
CNPhoneNumber CNLabeledValue 读取
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
{
NSArray *phoneNums = contact.phoneNumbers;
for (CNLabeledValue *labeledValue in phoneNums) {
// 2.1.获取电话号码的KEY
NSString *phoneLabel = labeledValue.label;
// 2.2.获取电话号码
CNPhoneNumber *phoneNumer = labeledValue.value;
NSString *phoneValue = phoneNumer.stringValue;
NSLog(@"%@ %@", phoneLabel, phoneValue);
}
}Contacts:遍历通讯录,不使用系统通讯录
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 1.获取授权状态
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
// 2.判断授权状态,如果不是已经授权,则直接返回
if (status != CNAuthorizationStatusAuthorized) return;
// 3.创建通信录对象
CNContactStore *contactStore = [[CNContactStore alloc] init];
// 4.创建获取通信录的请求对象
// 4.1.拿到所有打算获取的属性对应的key
NSArray *keys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
// 4.2.创建CNContactFetchRequest对象
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
// 5.遍历所有的联系人
[contactStore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
// 1.获取联系人的姓名
NSString *lastname = contact.familyName;
NSString *firstname = contact.givenName;
NSLog(@"%@ %@", lastname, firstname);
// 2.获取联系人的电话号码
NSArray *phoneNums = contact.phoneNumbers;
for (CNLabeledValue *labeledValue in phoneNums) {
// 2.1.获取电话号码的KEY
NSString *phoneLabel = labeledValue.label;
// 2.2.获取电话号码
CNPhoneNumber *phoneNumer = labeledValue.value;
NSString *phoneValue = phoneNumer.stringValue;
NSLog(@"%@ %@", phoneLabel, phoneValue);
}
}];
}Contacts:创建联系人
//创建联系人
-(void)initWithImage:(NSString *)imageName
andName:(NSArray *)name
andEmails:(NSArray *)email
andPhoneNumbers:(NSArray *)phoneNumber
andAddresses:(NSArray *)address
andBirthday:(NSArray *)birthdayArray
{
//=============格式化创建联系人=================
CNMutableContact *contact = [[CNMutableContact alloc] init];
//头像
contact.imageData = UIImagePNGRepresentation([UIImage imageNamed:imageName]);
//姓名
contact.familyName = name[0];
contact.givenName = name[1];
//邮箱
CNLabeledValue *homeEmail = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:email[0]];
CNLabeledValue *workEmail =[CNLabeledValue labeledValueWithLabel:CNLabelWork value:email[1]];
CNLabeledValue *otherEmail =[CNLabeledValue labeledValueWithLabel:CNLabelOther value:email[2]];
contact.emailAddresses = @[homeEmail,workEmail,otherEmail];
//邮箱键值
//家庭CNLabelHome 0
//工作CNLabelWork 1
//其他CNLabelOther 2
//电话
CNLabeledValue *iPhoneNumber = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberiPhone value:[CNPhoneNumber phoneNumberWithStringValue:phoneNumber[0]]];
CNLabeledValue *mobileNumber = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberiPhone value:[CNPhoneNumber phoneNumberWithStringValue:phoneNumber[1]]];
CNLabeledValue *mainNumber = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberiPhone value:[CNPhoneNumber phoneNumberWithStringValue:phoneNumber[2]]];
contact.phoneNumbers = @[iPhoneNumber,mobileNumber,mainNumber];
//电话键值
//CNLabelPhoneNumberiPhone 0
//CNLabelPhoneNumberMobile 1
//CNLabelPhoneNumberMain 2
//CNLabelPhoneNumberHomeFax
//CNLabelPhoneNumberWorkFax
//CNLabelPhoneNumberOtherFax
//CNLabelPhoneNumberPager
//地址 可设键值 home:[0]
CNMutablePostalAddress * homeAdress = [[CNMutablePostalAddress alloc]init];
homeAdress.street = address[0][2];
homeAdress.city = address[0][1];
homeAdress.state = address[0][0]; //国家
homeAdress.postalCode = address[0][3];
contact.postalAddresses = @[[CNLabeledValue labeledValueWithLabel:CNLabelHome value:homeAdress]];
//生日
NSDateComponents * birthday = [[NSDateComponents alloc]init];
birthday.day = (long int)birthdayArray[2];
birthday.month = (long)birthdayArray[1];
birthday.year = (long)birthdayArray[0];
contact.birthday=birthday;
//=============创建联系人请求=================
CNSaveRequest * saveRequest = [[CNSaveRequest alloc]init];
//添加联系人
[saveRequest addContact:contact toContainerWithIdentifier:nil];
// //更新一个联系人
// - (void)updateContact:(CNMutableContact *)contact;
// //删除一个联系人
// - (void)deleteContact:(CNMutableContact *)contact;
// //添加一组联系人
// - (void)addGroup:(CNMutableGroup *)group toContainerWithIdentifier:(nullable NSString *)identifier;
// //更新一组联系人
// - (void)updateGroup:(CNMutableGroup *)group;
// //删除一组联系人
// - (void)deleteGroup:(CNMutableGroup *)group;
// //向组中添加子组
// - (void)addSubgroup:(CNGroup *)subgroup toGroup:(CNGroup *)group NS_AVAILABLE(10_11, NA);
// //在组中删除子组
// - (void)removeSubgroup:(CNGroup *)subgroup fromGroup:(CNGroup *)group NS_AVAILABLE(10_11, NA);
// //向组中添加成员
// - (void)addMember:(CNContact *)contact toGroup:(CNGroup *)group;
// //向组中移除成员
// - (void)removeMember:(CNContact *)contact fromGroup:(CNGroup *)group;
//=============写操作=================
CNContactStore * store = [[CNContactStore alloc]init];
if([store executeSaveRequest:saveRequest error:nil])
NSLog(@"创建联系人成功。");
}
学习来源:
http://www.jianshu.com/p/df0ea100c3dahttp://my.oschina.net/u/2340880/blog/511995?fromerr=pOvvMWcv
这篇博客主要介绍了在iOS9中如何利用CNContact进行通讯录操作,包括使用ContactsUI展示系统通讯录视图,实现代理以读取联系人信息,详细讲解了CNPhoneNumber和CNLabeledValue在读取联系人电话号码中的应用,以及如何遍历和创建新的联系人。

3685

被折叠的 条评论
为什么被折叠?



