zoukankan      html  css  js  c++  java
  • 获取设备通讯录信息

    直接上代码:

    #import "ViewController.h"
    
    #import <Contacts/Contacts.h>
    
    #import <ContactsUI/ContactsUI.h>
    
    @interface ViewController ()<CNContactPickerDelegate>
    
    @property (nonatomic, strong) UIButton *btn;
    
    @property (nonatomic, strong) UILabel *label;
    
    @property (nonatomic, strong) UILabel *labelNum;
    
    
    @end
    
    @implementation ViewController
    
    
    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        _btn = [UIButton buttonWithType:UIButtonTypeSystem];
    
        _btn.frame = CGRectMake(10, 20, 50, 30);
    
        [_btn setTitle:@"按钮" forState:0];
    
        [_btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    
        [self.view addSubview:_btn];
    
        _label = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 50, 30)];
    
        [self.view addSubview:_label];
    
        _labelNum = [[UILabel alloc] initWithFrame:CGRectMake(10, 120, 50, 30)];
    
        [self.view addSubview:_labelNum];
    
    }
    
    
    - (void)btnAction {
    
    
        CNContactPickerViewController *contactPickerViewController = [[CNContactPickerViewController alloc] init];
    
        contactPickerViewController.delegate = self;
    
    
        [self presentViewController:contactPickerViewController animated:YES completion:nil];
    
    }
    
    // 如果实现该方法当选中联系人时就不会再出现联系人详情界面, 如果需要看到联系人详情界面只能不实现这个方法
    
    - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
    
        NSLog(@"选中某一个联系人时调用---------------------------------");
    
        [self printContactInfo:contact];
    
    }
    
    // 同时选中多个联系人
    - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact *> *)contacts {
    
        for (CNContact *contact in contacts) {
       NSLog(@"================================================");
    
            [self printContactInfo:contact];
    
        }
    
    }
    
    - (void)printContactInfo:(CNContact *)contact {
    
        NSString *givenName = contact.givenName;
    
        NSString *familyName = contact.familyName;
    
        NSLog(@"givenName=%@, familyName=%@", givenName, familyName);
    
        NSArray * phoneNumbers = contact.phoneNumbers;
    
        _label.text = familyName;
    
    
        for (CNLabeledValue<CNPhoneNumber*>*phone in phoneNumbers) {
    
            NSString *label = phone.label;
    
            CNPhoneNumber *phonNumber = (CNPhoneNumber *)phone.value;
    
            NSLog(@"label=%@, value=%@", label, phonNumber.stringValue);
    
            _labelNum.text = phonNumber.stringValue;
    
        }
    
    }
    
    
    // 注意:如果实现该方法,上面那个方法就不能实现了,这两个方法只能实现一个
    
    // - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {
    
    //  NSLog(@"选中某个联系人的某个属性时调用");
    
    // }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
        CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    
        if (authorizationStatus == CNAuthorizationStatusAuthorized) {
    
            NSLog(@"没有授权...");
    
    }
    
    
    // 获取指定的字段,并不是要获取所有字段,需要指定具体的字段
    
        NSArray *keysToFetch = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
    
        CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];
    
        CNContactStore *contactStore = [[CNContactStore alloc] init];
    
        [contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
    
            NSLog(@"-------------------------------------------------------");
    
            NSString *givenName = contact.givenName;
    
            NSString *familyName = contact.familyName;
    
            NSLog(@"givenName=%@, familyName=%@", givenName, familyName);
    
            NSArray *phoneNumbers = contact.phoneNumbers;
    
            for (CNLabeledValue *labelValue in phoneNumbers) {
            
    
                NSString *label = labelValue.label;
    
                CNPhoneNumber *phoneNumber = labelValue.value;
    
                NSLog(@"label=%@, phone=%@", label, phoneNumber.stringValue);
    
            }
    
            // *stop = YES; // 停止循环,相当于break;
    
        }];
    
    }
  • 相关阅读:
    分布式大数据高并发的web开发框架
    用户安全登录问题
    成功扩展live555支持ipv6,同时支持RTSPServer & RTSPClient
    经过两个多月的攻关,终于搞定了live555多线程并稳定压测通过
    经过两个多月的攻关,终于搞定了live555多线程并稳定压测通过
    如何使用EasyNVR+CDN突破萤石云在直播客户端数量上的限制,做到低成本高性价比的直播
    EasyNVR完美搭配腾讯云CDN/阿里云CDN进行RTMP、HLS直播加速的使用说明
    NVR硬件录像机web无插件播放方案功能实现之相关接口注意事项说明
    EasyNVR实现海康、大华NVR硬盘录像机Web无插件播放方案(支持取特定时间段视频流)
    EasyNVR无插件直播服务如何配合EasyBMS使用以及实现流媒体管理功能概述
  • 原文地址:https://www.cnblogs.com/xuzb/p/8677785.html
Copyright © 2011-2022 走看看