Hello?Contacts,say?goodbye?Address?Book?!
Contacts.framework 框架針對線程安全的只讀使用進(jìn)行了優(yōu)化。9.0以上才可以。
contact class 是線程安全的,contact class is like NSDictionary 并且有可變子類CNMutableContact。?you can create your own custom labels.?
不說多,先顯示一下 你的通訊錄
CNContactPickerViewController ?為顯示通訊錄的ViewController?
CNContactPickerDelegate 為代理 引入Contacts 和ContactsUI
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
@interface ViewController ()<CNContactPickerDelegate,CNContactViewControllerDelegate>
@end
- (void)viewDidLoad {
? ? [super viewDidLoad];
簡單顯示 你的通訊錄
CNContactPickerViewController *contactPickerViewController = [[CNContactPickerViewController alloc] init];
? ? // 設(shè)置代理 ? ?contactPickerViewController.delegate=self;
? ? // 顯示聯(lián)系人窗口視圖
? ? [self presentViewController:contactPickerViewController animated:YES completion:nil];
}
當(dāng)然啦,不能只顯示通訊錄。 按鈕時(shí)間都需要代理回調(diào)。
?//點(diǎn)擊聯(lián)系人控制器的Cancel按鈕執(zhí)行該方法
- (void)contactPickerDidCancel:(CNContactPickerViewController*)picker{
? ? NSLog(@"取消");
}
// 選中聯(lián)系人時(shí)執(zhí)行該方法
- (void)contactPicker:(CNContactPickerViewController*)picker didSelectContact:(CNContact*)contact{
? ? NSLog(@"聯(lián)系人的資料:%@",contact);
? ? [self dismissViewControllerAnimated:YES completion:nil];
? //顯示聯(lián)系人詳細(xì)頁面. ?這個(gè)頁面需要CNContactViewControllerDelegate 代理
? CNContactViewController *contactController = [CNContactViewController viewControllerForNewContact:contact];
? ? contactController.delegate=self;
? ? UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:contactController];
? ? [self settingNavBarForNavController:navController];
? ? [self? presentViewController:navController animated:YES completion:nil];
}
//當(dāng)用戶選擇屬性時(shí)調(diào)用
- (BOOL)contactViewController:(CNContactViewController*)viewController shouldPerformDefaultActionForContactProperty:(CNContactProperty*)property{
? ? return NO;
}
//該協(xié)議是在創(chuàng)建新的名片界面點(diǎn)擊取消或者確定后的回調(diào)
- (void)contactViewController:(CNContactViewController*)viewController didCompleteWithContact:(nullableCNContact*)contact{
? ? [viewControllerdismissViewControllerAnimated:YES completion:nil];
}