前言
Contacts.framework
是苹果推出的新联系人框架。应用于iOS9.0之后,9.0之后将会全面取代iOS 9 以前的通讯录框架 AddessBook.framework
。
手机通讯录iOS9前后对比 :
| iOS9之前| 描述 | iOS9之后 |描述 |
| -------------- | -------------- | -------------- |
|AddressBook| 纯 C 语言的 API,仅仅是获得联系人数据。没有提供 UI 界面展示,需要自己搭建联系人展示界面。 | Contacts|拥有 AddressBookUI 框架的所有功能,使用起来更加的面向对象。|
| AddressBookUI |提供了联系人列表界面、联系人详情界面、添加联系人界面等,一般用于选择联系人| ContactsUI |拥有 AddressBook框架的所有功能,不再是 C 语言的 API,使用简单。|
通讯录框架ContactsUI使用
1、导入框架
#import <ContactsUI/ContactsUI.h>
2、遵循代理
<CNContactPickerDelegate>
代理方法:
/*!
* @abstract Invoked when the picker is closed.
* @discussion The picker will be dismissed automatically after a contact or property is picked.
*/
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker;
/*!
* @abstract Singular delegate methods.
* @discussion These delegate methods will be invoked when the user selects a single contact or property.
*/
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;
/*!
* @abstract Plural delegate methods.
* @discussion These delegate methods will be invoked when the user is done selecting multiple contacts or properties.
* Implementing one of these methods will configure the picker for multi-selection.
*/
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact*> *)contacts;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperties:(NSArray<CNContactProperty*> *)contactProperties;
3、授权
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusNotDetermined) {
[[[CNContactStore alloc] init] requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"点击同意");
[self openContact];
}else{
NSLog(@"点击拒绝");
}
}];
} else if (status == CNAuthorizationStatusAuthorized) {
NSLog(@"已经授权");
[self openContact];
} else {
NSLog(@"没有授权");
UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"We Need Permission To Access Your Contacts"
message:@"Settings on iphone >Privacy>Photos>seagullstudio" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Setting" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{UIApplicationOpenSettingsURLString:@YES} completionHandler:^(BOOL success) {
if (success) {
// NSLog(@"成功打开");
} else {
// NSLog(@"打开失败");
}
}];
}]];
[self presentViewController:alert animated:YES completion:nil];
}
4、获取通讯录控制器
- (void)openContact {
CNContactPickerViewController *contactVC = [CNContactPickerViewController new];
contactVC.delegate = self;
[self presentViewController:contactVC animated:YES completion:nil];
}
5、实现delegate方法,获取联系人地址
#define kNULLString(string) ((![string isKindOfClass:[NSString class]])||[string isEqualToString:@""] || (string == nil) || [string isEqualToString:@""] || [string isKindOfClass:[NSNull class]]||[[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)
//选择单个联系人,不展开联系人的详细信息,获取后推出CNContactPickerViewController
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
[self parseAddressWithContact:contact];
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)parseAddressWithContact:(CNContact *)contact {
//contact的结构分析我贴在文章底部
NSArray<CNLabeledValue *> *postAddressArray = contact.postalAddresses;
for (CNLabeledValue *addressLabel in postAddressArray) {
CNPostalAddress *address = addressLabel.value;
if (!kNULLString(address.street)) {
NSLog(@"%@", address.street );
}
if (!kNULLString(address.city)) {
NSLog(@"%@", address.city );
}
if (!kNULLString(address.postalCode)) {
NSLog(@"%@", address.postalCode );
}
if (!kNULLString(address.country)) {
NSLog(@"%@", address.country );
}
if (!kNULLString(address.state)) {
NSLog(@"%@", address.state );
}
}
}
//取消选择的回调
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
写在最后
感谢:
JoySeeDog