iOS调用系统的通讯录界面获取用户信息

虽然目前出了新的通讯录接口 <ContactsUI/ContactsUI.h>但是目前还有很多用户没有升级到9.0因此使用#import <AddressBook/AddressBook.h>#import <AddressBookUIAddressBookUI.h>还是有必要。我在下一篇博客将会研究 <ContactsUI/ContactsUI.h>

设置代理

@interface ViewController ()<CNContactPickerDelegate,CNContactViewControllerDelegate><ABPeoplePickerNavigationControllerDelegate,UINavigationControllerDelegate>

- (IBAction)choosePerson:(id)sender {
    ABPeoplePickerNavigationController *peoplePickController = [[ABPeoplePickerNavigationController alloc] init];
    peoplePickController.peoplePickerDelegate = self;
    [self presentViewController:peoplePickController animated:YES completion:^{

    }];

}

不要弄错是peoplePickerDelegate不是delegate。我一开始弄错了。搞了很久。。

实现代理

点击取消按钮的时候调用这个方法

// Called after the user has pressed cancel.
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{

}

下面两个方法最好是只实现一个。因为只有一个会被调用。

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person {
    ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFStringRef telValue = ABMultiValueCopyValueAtIndex(valuesRef,0);
    self.label.text = (__bridge NSString *)telValue;

}

上面这个方法,只进入列表就可以返回电话号码了。
进入这个界面点击就可以返回数据了
电话列表
如果你实现了下面这个方法,则上面这个方法不会被调用。

// Called after a property has been selected by the user.
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0){
    ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFStringRef telValue = ABMultiValueCopyValueAtIndex(valuesRef,0);
    self.label.text = (__bridge NSString *)telValue;
}

两个方法的区别在于,第一个只进入到列表。第二个可以进入到具体的属性页面。就是到这来点号码后会被调用
号码详情
可以自己打断点试一下。只要实现了第二个,第一个便不会调用了。

博客地址iseedog

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值