#import <ContactsUI/ContactsUI.h>
遵循代理
CNContactPickerDelegate
调用通讯录
如果在iOS10的机器上调用以前的ABPeoplePickerNavigationController老方法将直接崩溃.
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //iOS 10 // AB_DEPRECATED("Use CNContactPickerViewController from ContactsUI.framework instead") CNContactPickerViewController * contactVc = [CNContactPickerViewController new]; contactVc.delegate = self; [self presentViewController:contactVc animated:YES completion:^{ }]; }
选择完成代理回调
-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{ NSLog(@"name:%@%@",contact.familyName,contact.givenName); CNLabeledValue * labValue = [contact.phoneNumbers lastObject]; NSLog(@"phone:%@",[labValue.value stringValue]); }
取消选择回调
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker{ [picker dismissViewControllerAnimated:YES completion:nil]; }