iOS 调用系统通讯录 获取联系人信息
iOS7 iOS8 使用ABPeoplePickerNavigationController 获取联系人信息
iOS9 系统推荐 CNContactPickerViewController 获取联系人信息
如出现 “plugin com.apple.MobileAddressBook.ContactsViewService invalidated” 警告,请将对象设置为全局变量即可
static ABPeoplePickerNavigationController *peoplePicker;
static CNContactPickerViewController *contactPicker;
iOS7、iOS8 代码
// new
peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
peoplePicker.displayedProperties = @[@(kABPersonPhoneProperty)];
peoplePicker.peoplePickerDelegate = self;
[viewController presentViewController:peoplePicker animated:YES completion:NULL];
#pragma mark - ABPeoplePickerNavigationControllerDelegate
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)picker
{
[peoplePicker dismissViewControllerAnimated:YES completion:NULL];
}
// use in iOS7 or earlier
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
return YES;
}
// use in iOS7 or earlier
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
[self handleSelectPerson:person property:property identifier:identifier];
return NO;
}
// use in iOS8
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
[self handleSelectPerson:person property:property identifier:identifier];
}
// handle
- (void)handleSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
[peoplePicker dismissViewControllerAnimated:YES completion:NULL];
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, property);
NSString *phone = nil;
if ((ABMultiValueGetCount(phoneNumbers) > 0)) {
phone = (NSString *)CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, identifier));
}
if (phone == nil) {
phone = @"";
}
NSString *phoneStr = [NSString stringWithString:phone];
NSString *firstName = (NSString *)CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = (NSString *)CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
if (!firstName) {
firstName = @"";
}
if (!lastName) {
lastName = @"";
}
NSString *nameStr = [NSString stringWithFormat:@"%@%@",lastName,firstName];
[self handleCallbackWithName:nameStr withPhone:phoneStr];
}
iOS9 代码
// new
contactPicker = [[CNContactPickerViewController alloc] init];
contactPicker.displayedPropertyKeys = @[CNContactPhoneNumbersKey];
contactPicker.delegate = self;
[viewController presentViewController:contactPicker animated:YES completion:NULL];
#pragma mark - CNContactPickerDelegate
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker
{
[contactPicker dismissViewControllerAnimated:YES completion:NULL];
}
// use in iOS9
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
{
[contactPicker dismissViewControllerAnimated:YES completion:NULL];
NSString *phone = [contactProperty.value stringValue];
if (phone == nil) {
phone = @"";
}
NSString *phoneStr = [NSString stringWithString:phone];
NSString *firstName = contactProperty.contact.familyName;
NSString *lastName = contactProperty.contact.givenName;
if (!firstName) {
firstName = @"";
}
if (!lastName) {
lastName = @"";
}
NSString *nameStr = [NSString stringWithFormat:@"%@%@",firstName,lastName];
[self handleCallbackWithName:nameStr withPhone:phoneStr];
}