iOS 调用系统通讯录 获取联系人信息

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];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值