之前我写过一篇通讯录开发的文章,很多同学问我要demo,当时太忙没来得及写,现在有空谢了,发现通讯录框架在iOS9下已经升级了。
在以前iOS开发中,涉及联系人相关的编程,代码都非常繁琐,并且框架的设计也不是Objective-C风格的,这使开发者用起来非常的难受。在iOS9中,apple终于解决了这个问题,全新的Contacts Framework将完全替代AddressBookFramework。
一、联系人对象CNContact
创建CNMutableContact对象:
CNMutableContact * contact = [[CNMutableContact alloc] init];
设置联系人头像:
contact.imageData = UIImagePNGRepresentation([UIImage imageNamed:@"Icon.png"]);
设置联系人姓名:
//设置名字
contact.givenName = @"stephen";
//设置姓氏
contact.familyName = @"zhuang";
设置联系人邮箱:
CNLabeledValue *homeEmail = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:@"[email protected]"];
CNLabeledValue *workEmail =[CNLabeledValue labeledValueWithLabel:CNLabelWork value:@"[email protected]"];
contact.emailAddresses = @[homeEmail,workEmail];
这里需要注意,emailAddresses属性是一个数组,数组中是才CNLabeledValue对象,CNLabeledValue对象主要用于创建一些联系人属性的键值对应,通过这些对应,系统会帮我们进行数据的格式化,例如CNLabelHome,就会将号码格式成家庭邮箱的格式,相应的其他键如下:
//家庭
CONTACTS_EXTERN NSString * const CNLabelHome NS_AVAILABLE(10_11, 9_0);
//工作
CONTACTS_EXTERN NSString * const CNLabelWork NS_AVAILABLE(10_11, 9_0);
//其他
CONTACTS_EXTERN NSString * const CNLabelOther NS_AVAILABLE(10_11, 9_0);
// 邮箱地址
CONTACTS_EXTERN NSString * const CNLabelEmailiCloud NS_AVAILABLE(10_11, 9_0);
// url地址
CONTACTS_EXTERN NSString * const CNLabelURLAddressHomePage NS_AVAILABLE(10_11, 9_0);
// 日期
CONTACTS_EXTERN NSString * const CNLabelDateAnniversary NS_AVAILABLE(10_11, 9_0);
设置联系人电话:
contact.phoneNumbers = @[[CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberiPhone value:[CNPhoneNumber phoneNumberWithStringValue:@"12344312321"]]];
联系人电话的配置方式和邮箱类似,键值如下:
CONTACTS_EXTERN NSString * const CNLabelPhoneNumberiPhone NS_AVAILABLE(10_11, 9_0);
CONTACTS_EXTERN NSString * const CNLabelPhoneNumberMobile NS_AVAILABLE(10_11, 9_0);
CONTACTS_EXTERN NSString * const CNLabelPhoneNumberMain NS_AVAILABLE(10_11, 9_0);
CONTACTS_EXTERN NSString * const CNLabelPhoneNumberHomeFax NS_AVAILABLE(10_11, 9_0);
CONTACTS_EXTERN NSString * const CNLabelPhoneNumberWorkFax NS_AVAILABLE(10_11, 9_0);
CONTACTS_EXTERN NSString * const CNLabelPhoneNumberOtherFax NS_AVAILABLE(10_11, 9_0);
CONTACTS_EXTERN NSString * const CNLabelPhoneNumberPager NS_AVAILABLE(10_11, 9_0);
设置联系人地址:
CNMutablePostalAddress * homeAdress = [[CNMutablePostalAddress alloc]init];
homeAdress.street = @"";
homeAdress.city = @"";
homeAdress.state = @"";
homeAdress.postalCode = @"";
contact.postalAddresses = @[[CNLabeledValue labeledValueWithLabel:CNLabelHome value:homeAdress]];
设置生日:
NSDateComponents * birthday = [[NSDateComponents alloc]init];
birthday.day=2;
birthday.month=11;
birthday.year=1989;
contact.birthday=birthday;
二、联系人请求: CNSaveRequest
//初始化方法
CNSaveRequest * saveRequest = [[CNSaveRequest alloc]init];
//添加联系人
[saveRequest addContact:contact toContainerWithIdentifier:nil];
@interface CNSaveRequest : NSObject
//添加一个联系人
- (void)addContact:(CNMutableContact *)contact toContainerWithIdentifier:(nullable NSString *)identifier;
//更新一个联系人
- (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;
@end
写入操作:
CNContactStore * store = [[CNContactStore alloc]init];
[store executeSaveRequest:saveRequest error:nil];
三、获取联系人信息
// 创建通信录对象
CNContactStore *contactStore = [[CNContactStore alloc] init];
// 创建获取通信录的请求对象
// 拿到所有打算获取的属性对应的key
NSArray *keys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
// 创建CNContactFetchRequest对象
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
// 遍历所有的联系人
[contactStore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
// 获取联系人的姓名
NSString *lastname = contact.familyName;
NSString *firstname = contact.givenName;
NSLog(@"%@ %@", lastname, firstname);
// 获取联系人的电话号码
NSArray *phoneNums = contact.phoneNumbers;
for (CNLabeledValue *labeledValue in phoneNums) {
// 获取电话号码的KEY
NSString *phoneLabel = labeledValue.label;
// 获取电话号码
CNPhoneNumber *phoneNumer = labeledValue.value;
NSString *phoneValue = phoneNumer.stringValue;
NSLog(@"%@ %@", phoneLabel, phoneValue);
ZXPersonTemp *person = [[ZXPersonTemp alloc] init];
person.name = [NSString stringWithFormat:@"%@%@",lastname,firstname];
person.phone = phoneValue;
[_addressBookArray addObject:person];
}
}];
四、选择联系人
- (void)insertNewObject:(id)sender {
CNContactPickerViewController * con = [[CNContactPickerViewController alloc] init];
con.delegate = self;
[self presentViewController:con animated:YES completion:nil];
}
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(nonnull CNContactProperty *)contactProperty
{
CNContact *contact = contactProperty.contact;
CNPhoneNumber *phoneNumer = contactProperty.value;
NSString *phoneValue = phoneNumer.stringValue;
NSString *lastname = contact.familyName;
NSString *firstname = contact.givenName;
ZXPersonTemp *person = [[ZXPersonTemp alloc] init];
person.name = [NSString stringWithFormat:@"%@%@",lastname,firstname];
person.phone = phoneValue;
[_addressBookArray insertObject:person atIndex:0];
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
[picker dismissViewControllerAnimated:YES completion:nil];
}
最后附上一个demo,戳我,效果如下: