在iOS10中使用ABPeoplePicker获取联系人电话号码时,直接crash。
在stackoverflowstackoverflower地址中搜到,这个Address Book API 在iOS9已经被废弃,用Contacts Framework替代。
这样只需要把ABPeoplePickerViewController替换为CNContactPickerViewController。
具体代码实现如下:
- (void)moreIos9ContactPicker
{
CNContactPickerViewController *cnController = [CNContactPickerViewController new];
cnController.delegate = self;
NSArray *arrKeys = @[CNContactPhoneNumbersKey];
cnController.displayedPropertyKeys = arrKeys;
[self presentViewController:cnController animated:YES completion:nil];
}
#pragma mark - CNContactPickerDelegate
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
{
CNPhoneNumber *phoneNumber = contactProperty.value;
NSString *phoneNO = phoneNumber.stringValue;
[self getPhoneNumber:phoneNO];
}
- (void)getPhoneNumber:(NSString *)phoneNO
{
if ([phoneNO hasPrefix:@"+"]) {
phoneNO = [phoneNO substringFromIndex:3];
}
phoneNO = [phoneNO stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"%@", phoneNO);
}