一 前言
最近项目要求 获取用户通讯录 这一功能,发现以前的AddressBook框架已经被弃用了,iOS 9.0 以后改为Contacts框架。但是网上大多是swift版本,OC版本较少。花了一些时间,简单整理了这个通讯录功能,基本能满足项目要求。
二 添加联系人
在使用前,先在项目中导入ContactsUI.framework 和Contacts.framework两个框架。接着在需要使用的页面导入:
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
添加联系人功能:
CNMutableContact *contact=[[CNMutableContact alloc]init];
contact.givenName=@"san";
contact.familyName=@"zhang";
//联系人邮箱
CNLabeledValue *homeEmail=[CNLabeledValue labeledValueWithLabel:CNLabelHome value:@"9999999@qq.com"];
CNLabeledValue *workEmail=[CNLabeledValue labeledValueWithLabel:CNLabelWork value:@"9999999@qq.com"];
contact.emailAddresses=@[homeEmail,workEmail];
//添加 联系人电话
contact.phoneNumbers=@[[CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberiPhone value:[CNPhoneNumber phoneNumberWithStringValue:@"15555555555"]]];
CNMutablePostalAddress *homeAdress=[[CNMutablePostalAddress alloc]init];
homeAdress.street=@"狮山路";
homeAdress.city=@"苏州";
homeAdress.state=@"中国";
homeAdress.postalCode=@"215100";
contact.postalAddresses=@[[CNLabeledValue labeledValueWithLabel:CNLabelHome value:homeAdress]];
NSDateComponents *birth=[[NSDateComponents alloc]init];
birth.day=7;
birth.month=3;
birth.year=1994;
contact.birthday=birth;
//添加请求
CNSaveRequest *saveRequest=[[CNSaveRequest alloc]init];
[saveRequest addContact:contact toContainerWithIdentifier:nil];
//写入
CNContactStore *store=[[CNContactStore alloc]init];
[store executeSaveRequest:saveRequest error:nil];
三 获取联系人
方法1:
NSString *foematter=[CNContactFormatter stringFromContact:contact style:CNContactFormatterStyleFullName];
NSLog(@"11:::%@",foematter);
NSString *foematter2=[CNPostalAddressFormatter stringFromPostalAddress:homeAdress style:CNPostalAddressFormatterStyleMailingAddress];
NSLog(@"foematter222:;%@",foematter2);
方法2:
CNContactStore *store2=[[CNContactStore alloc]init];
CNContactFetchRequest *request=[[CNContactFetchRequest alloc]initWithKeysToFetch:@[CNContactPhoneticFamilyNameKey,CNContactPhoneNumbersKey]];
[store2 enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
NSLog(@"store::2%@",contact.phoneNumbers);
}];
四 读取联系人
首先是跳转到联系人页面:
CNContactPickerViewController *contactview=[[CNContactPickerViewController alloc]init];
contactview.delegate=self;
[self presentViewController:contactview animated:YES completion:nil];
然后执行代理方法,当点击某一属性时,获取相应的信息:
[html] view plain copy
-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
[html] view plain copy
if ([contactProperty.key isEqualToString:@"phoneNumbers"]) {
CNPhoneNumber *phonenumber=contactProperty.value;//将value转为cnphonenumber类型
NSLog(@"%@",phonenumber.stringValue);
}
else if ([contactProperty.key isEqualToString:@"postalAddresses"])
{
CNPostalAddress *address=contactProperty.value;
NSLog(@"%@",address.street);
}
else
{
NSLog(@"%@",contactProperty.value);
}
NSString *str=[NSString stringWithFormat:@"%@ %@",contactProperty.contact.familyName,contactProperty.contact.givenName];
NSLog(@"str:%@",str);
值得注意的是,contact property 是以键值的形式存储用户信息的,当读取时,也可以根据相应的key取值。
CNContactPickerDelegate 还有其他代理方法:
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact;
[html] view plain copy
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact*> *)contacts;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperties:(NSArray<CNContactProperty*> *)contactProperties;