如何使用 Contacts.framework 打开 iPhone 通讯录

How to open iPhone address book using Contacts.framework

我正在尝试使用新 Contacts.framework 打开地址簿。这个框架是在 iOS 9.0 中引入的,我尝试了一些打开视图控制器的方法但最终崩溃了。有人遇到过类似的问题吗?

NSError *error;

CNContactStore *store = [[CNContactStore alloc] init];
NSArray *cArray = [store unifiedContactsMatchingPredicate:[CNContact predicateForContactsMatchingName:@"Kate"] keysToFetch:@[CNContactEmailAddressesKey,CNContactPhoneNumbersKey] error:&error];

CNContactViewController *cVC = [CNContactViewController viewControllerForContact:[cArray objectAtIndex:0]];
[self presentViewController:cVC animated:YES completion:^{

}];

因为 Contacts.framework 仅在 iOS 9.0 之后可用。这是检查可用性和启动联系人选择器视图控制器的代码。

调用联系人选择器视图控制器的代码

if ([CNContactPickerViewController class]) {
     CNContactPickerViewController *cVC = [[CNContactPickerViewController alloc] init];
     [self.view.window.rootViewController presentViewController:cVC animated:YES completion:nil];
}

Swift 4.x

let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
self.present(contactPicker, animated: true, completion: nil)