iOS通过访问系统通讯录,获取选择用户的全名和电话
2018-07-20 来源:open-open
#import "ViewController.h" #import <AddressBookUI/AddressBookUI.h> @interface ViewController ()<ABPeoplePickerNavigationControllerDelegate> @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } #pragma mark 点击获取指定练习人的 姓名/电话 - (IBAction)btnclick:(UIButton *)sender { //1. 创建联系人选择导航控制器 ABPeoplePickerNavigationController *picker = [ABPeoplePickerNavigationController new]; //2. 设置代理 注意不要设置错了 picker.peoplePickerDelegate = self; //3. 以模态视图弹出 [self presentViewController:picker animated:YES completion:nil]; } #pragma mark 实现代理方法 /// 当选择了联系人的时候调用此方法 /// /// @param person 选中的联系人 - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person { //获取联系人姓名 //NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty)); // NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty)); //self.nameLabel.text = [lastName stringByAppendingString:firstName]; NSString *fullName = CFBridgingRelease(ABRecordCopyCompositeName(person)); self.nameLabel.text = fullName; //获取联系人电话 ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty); //遍历phones, 拿到每一个电话 CFIndex count = ABMultiValueGetCount(phones); for (int i = 0; i < count; i++) { // 电话label NSString *label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, i)); // 电话 NSString *value = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i)); NSLog(@"label:%@, value: %@",label, value); } //释放CF对象.如果没有纪录电话,phone是nil,不能释放。 if (phones != nil) { CFRelease(phones); } } @end
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
下一篇: JDK7目录监视服务
最新资讯
热门推荐