IOS-原生通讯录Contact

作者: 温学振 | 来源:发表于2017-12-21 14:10 被阅读35次

    为什么要用CNContactPickerViewController啊??因为ABPeoplePickerViewController被废弃了啊 虽然可以用但是满足不了强迫症。不废话了!

    CNContact使用这个框架的时候大多情况下使用到了KVC

    • 1、在Build Phases中的Link Binary With Libraries里导入 Contacts.framework和ContactsUI.framework
    • 2、修改info.plist里的权限,key:Privacy - Contacts Usage Description Value:要说明获取通讯录的目的
    • 3、上代码啊
    #import <ContactsUI/ContactsUI.h>
    #import <Contacts/Contacts.h>
    @interface ViewController ()<CNContactPickerDelegate>
    
    @end
    
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 150, 50)];
        btn.titleLabel.font = [UIFont systemFontOfSize:10.f];
        [btn setTitle:@"model出通讯录" forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor redColor];
        [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        
        UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(200, 200, 150, 50)];
        btn1.titleLabel.font = [UIFont systemFontOfSize:10.f];
        [btn1 setTitle:@"授权授按一次添加1次" forState:UIControlStateNormal];
        btn1.backgroundColor = [UIColor greenColor];
        [btn1 addTarget:self action:@selector(btn1Click) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn1];
    }
    @end
    

    btn是Model出通讯录的按钮,btn1是添加联系人的按钮

    • 4、 btn按钮的响应,Model出本地通讯录
    -(void)btnClick{
        NSLog(@"%s",__func__);
        //1、创建选择联系人界面
        //这个框架会自动获取授权
        CNContactPickerViewController *CnNav = [[CNContactPickerViewController alloc] init];
        //2、设置代理
        CnNav.delegate = self;
        //3、弹出选择联系人界面
        [self presentViewController:CnNav animated:YES completion:nil];
        //4、实现代理方法
        //5、在对应的代理方法中获取联系人信息
    }
    

    以上代码会在控制器加载完毕后model出一个手机的通讯录,可以选择联系人查看对应的属性,拨打电话。

    • 5、CNContactPickerDelegate代理
    //选中某个联系人后 得到contact内的一些属性,但是实现该代理会dismiss掉CNContactPickerViewController
    - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
        NSLog(@"contact信息:%@",contact);
        NSString *Fullname = [CNContactFormatter stringFromContact:contact style:CNContactFormatterStyleFullName];
        NSLog(@"Fullname:%@",Fullname);
    }
    
    //选中某个联系人的某个属性 能看到属性 能得到具体属性
    //contactProperty是以键值的形式存储用户信息的,当读取时,也可以根据相应的key取值。
    - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {
        if ([contactProperty.key isEqualToString:@"phoneNumbers"]) {
            CNPhoneNumber *phonenumber = contactProperty.value;//将value转为CNPhoneNumber
            NSLog(@"phonenumber.stringValue:%@",phonenumber.stringValue);
        }else if ([contactProperty.key isEqualToString:@"postalAddresses"]){
            CNPostalAddress *address = contactProperty.value;
            NSLog(@"address.street:%@\naddress.state:%@",address.street,address.state);
        }else{
            NSLog(@"contactProperty.value:%@",contactProperty.value);
        }
    
        NSString *str = [NSString stringWithFormat:@"contactProperty.contact.familyName:%@  contactProperty.contact.givenName:%@",contactProperty.contact.familyName,contactProperty.contact.givenName];
        NSLog(@"str:%@",str);
    }
    
    ////选中多个联系人
    - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact *> *)contacts{
        NSLog(@"contactscontacts:%@",contacts);
    }
    
    //选中1个联系人的多个属性
    - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperties:(NSArray<CNContactProperty *> *)contactProperties{
        NSLog(@"contactProperties:%@",contactProperties);
    }
    
    //点击右上角取消的时候触发
    - (void)contactPickerDidCancel:(CNContactPickerViewController *)picker{
        NSLog(@"cancel");
    }
    
    • 6、btn1按钮的响应 ,创建联系人
    //通过CNLabeledValue类下的字典labeledValueWithLabel:key value:@""创建新的列表
    -(void)btn1Click{
        NSLog(@"%s",__func__);
        NSLog(@"新建联系人");
        CNMutableContact *contact = [[CNMutableContact alloc] init];
        contact.givenName = @"give名字";
        contact.familyName = @"伐木累名字";
        
        //添加联系人邮箱
        CNLabeledValue *homeEmail = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:@"homeEmail@qq.com"];
        CNLabeledValue *workEmail = [CNLabeledValue labeledValueWithLabel:CNLabelWork value:@"workEmail@qq.com"];
        contact.emailAddresses = @[homeEmail,workEmail];
        
        //添加联系人电话
        contact.phoneNumbers = @[[CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberiPhone value:[CNPhoneNumber phoneNumberWithStringValue:@"133333333333"]]];
        
        //添加联系人地址
        CNMutablePostalAddress *homeAdress = [[CNMutablePostalAddress alloc] init];
        homeAdress.street = @"什么路";
        homeAdress.city = @"什么城市";
        homeAdress.state = @"什么区域 指国家";
        homeAdress.postalCode = @"邮箱";
        contact.postalAddresses = @[[CNLabeledValue labeledValueWithLabel:CNLabelHome value:homeAdress]];
        
        //添加联系人生日
        NSDateComponents *birth = [[NSDateComponents alloc] init];
        birth.day = 7;
        birth.month = 3;
        birth.year = 1994;
        contact.birthday = birth;
        
        //添加请求 要在info里设置contact允许
        CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
        [saveRequest addContact:contact toContainerWithIdentifier:nil];
        
        //写入通讯录
        CNContactStore *store = [[CNContactStore alloc] init];
        [store executeSaveRequest:saveRequest error:nil];
     }
    
    

    相关文章

      网友评论

        本文标题:IOS-原生通讯录Contact

        本文链接:https://www.haomeiwen.com/subject/xlhnwxtx.html