iOS通过访问系统通讯录,获取选择用户的全名和电话详解手机开发

#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 
 
 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/3319.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论