#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]; }