iOS - ABPeoplePickerNavigationController 获取手机通讯录信息

最近项目用到获取用户手机通讯录的功能,但发现有两个代理方法在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);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值