我正在尝试使用新的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)