之前我寫過一篇通訊錄開發(fā)的文章估蹄,很多同學(xué)問我要demo,當(dāng)時太忙沒來得及寫荠列,現(xiàn)在有空謝了类浪,發(fā)現(xiàn)通訊錄框架在iOS9下已經(jīng)升級了。
在以前iOS開發(fā)中肌似,涉及聯(lián)系人相關(guān)的編程费就,代碼都非常繁瑣,并且框架的設(shè)計也不是Objective-C風(fēng)格的川队,這使開發(fā)者用起來非常的難受力细。在iOS9中,apple終于解決了這個問題固额,全新的Contacts Framework將完全替代AddressBookFramework艳汽。
一、聯(lián)系人對象CNContact
創(chuàng)建CNMutableContact對象:
CNMutableContact * contact = [[CNMutableContact alloc] init];
設(shè)置聯(lián)系人頭像:
contact.imageData = UIImagePNGRepresentation([UIImage imageNamed:@"Icon.png"]);
設(shè)置聯(lián)系人姓名:
//設(shè)置名字
contact.givenName = @"stephen";
//設(shè)置姓氏
contact.familyName = @"zhuang";
設(shè)置聯(lián)系人郵箱:
CNLabeledValue *homeEmail = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:@"379128008@qq.com"];
CNLabeledValue *workEmail =[CNLabeledValue labeledValueWithLabel:CNLabelWork value:@"379128008@qq.com"];
contact.emailAddresses = @[homeEmail,workEmail];
這里需要注意对雪,emailAddresses屬性是一個數(shù)組河狐,數(shù)組中是才CNLabeledValue對象,CNLabeledValue對象主要用于創(chuàng)建一些聯(lián)系人屬性的鍵值對應(yīng)瑟捣,通過這些對應(yīng)馋艺,系統(tǒng)會幫我們進(jìn)行數(shù)據(jù)的格式化,例如CNLabelHome迈套,就會將號碼格式成家庭郵箱的格式捐祠,相應(yīng)的其他鍵如下:
//家庭
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);
設(shè)置聯(lián)系人電話:
contact.phoneNumbers = @[[CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberiPhone value:[CNPhoneNumber phoneNumberWithStringValue:@"12344312321"]]];
聯(lián)系人電話的配置方式和郵箱類似,鍵值如下:
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);
設(shè)置聯(lián)系人地址:
CNMutablePostalAddress * homeAdress = [[CNMutablePostalAddress alloc]init];
homeAdress.street = @"";
homeAdress.city = @"";
homeAdress.state = @"";
homeAdress.postalCode = @"";
contact.postalAddresses = @[[CNLabeledValue labeledValueWithLabel:CNLabelHome value:homeAdress]];
設(shè)置生日:
NSDateComponents * birthday = [[NSDateComponents alloc]init];
birthday.day=2;
birthday.month=11;
birthday.year=1989;
contact.birthday=birthday;
二桑李、聯(lián)系人請求: CNSaveRequest
//初始化方法
CNSaveRequest * saveRequest = [[CNSaveRequest alloc]init];
//添加聯(lián)系人
[saveRequest addContact:contact toContainerWithIdentifier:nil];
@interface CNSaveRequest : NSObject
//添加一個聯(lián)系人
- (void)addContact:(CNMutableContact *)contact toContainerWithIdentifier:(nullable NSString *)identifier;
//更新一個聯(lián)系人
- (void)updateContact:(CNMutableContact *)contact;
//刪除一個聯(lián)系人
- (void)deleteContact:(CNMutableContact *)contact;
//添加一組聯(lián)系人
- (void)addGroup:(CNMutableGroup *)group toContainerWithIdentifier:(nullable NSString *)identifier;
//更新一組聯(lián)系人
- (void)updateGroup:(CNMutableGroup *)group;
//刪除一組聯(lián)系人
- (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];
三踱蛀、獲取聯(lián)系人信息
// 創(chuàng)建通信錄對象
CNContactStore *contactStore = [[CNContactStore alloc] init];
// 創(chuàng)建獲取通信錄的請求對象
// 拿到所有打算獲取的屬性對應(yīng)的key
NSArray *keys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
// 創(chuàng)建CNContactFetchRequest對象
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
// 遍歷所有的聯(lián)系人
[contactStore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
// 獲取聯(lián)系人的姓名
NSString *lastname = contact.familyName;
NSString *firstname = contact.givenName;
NSLog(@"%@ %@", lastname, firstname);
// 獲取聯(lián)系人的電話號碼
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];
}
}];
四窿给、選擇聯(lián)系人
- (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,戳我率拒,效果如下:
addressbook.gif