最近项目用到获取用户手机通讯录的功能,但发现有两个代理方法在iOS 9.0 废弃了,用新的代理方法代替,所以整理下供以后参考。。
#pragma mark - 点击联系人 链接到用户手机通讯录
- (void)accessPhoneBook: (UIButton *)sender {ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc]init];
picker.peoplePickerDelegate = self;
[picker setHidesBottomBarWhenPushed:YES];
[self presentViewController:picker animated:YES completion:nil];
}
- (void)displayPerson:(ABRecordRef)person {
NSString* phone = nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
}
else {
phone = @"[None]";
}
self.textFieldView.text = phone;
self.textFieldView.text = [self.textFieldView.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
self.phoneNum = [phone stringByReplacingOccurrencesOfString:@"-" withString:@""];
CFRelease(phoneNumbers);
}
#pragma mark - ABPeoplePickerNavigationControllerDelegate - iOS 9.0 new
// iOS 9.0 new
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {
[self displayPerson:person];
[self dismissViewControllerAnimated:YES completion:nil];
}
// iOS 9.0 new
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark
PS 之前被废弃的代理方法是:
// Deprecated, use predicateForSelectionOfPerson and/or -peoplePickerNavigationController:didSelectPerson: instead.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person NS_DEPRECATED_IOS(2_0, 8_0);
// Deprecated, use predicateForSelectionOfProperty and/or -peoplePickerNavigationController:didSelectPerson:property:identifier: instead.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_DEPRECATED_IOS(2_0, 8_0);