关于ios9中得AddressBook和AddressBookUI框架过时问题

ios9的更新后,AddressBook.framework和AddressBookUI.framework几乎是全部过时,其不支持OC。用Contacts.framework和ContactsUI.framework代替。

显示联系人:

    CNContactPickerViewController *contactPickerViewController=[[CNContactPickerViewController alloc]init];
    //该对象保存整个通讯录,可以遍历出来
    CNContactStore *contactStore=[[CNContactStore alloc]init];
    
    //判断是否授予过权限。(应用第一次打开通讯录一般运行)
    if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]==CNAuthorizationStatusNotDetermined) {
        //如果没有,弹窗申请权限
        [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
            //如果允许
            if (granted) {
                NSLog(@"权限允许");
                [self presentViewController:contactPickerViewController animated:YES completion:^{
                }];
                
            }
            //不允许
            else
            {
                NSLog(@"权限不允许");
            }
        }];
    }
    //如果不是第一次访问通讯录,判断是否有允许访问
    if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusAuthorized) {
        NSLog(@"有权限访问");
        [self presentViewController:contactPickerViewController animated:YES completion:^{
        }];
    }
    //不允许访问
    else
    {
        UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"提醒" message:@"没有权限访问,请开启权限" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [alertController addAction:action];
        [self presentViewController:alertController animated:YES completion:nil];
        NSLog(@"没有权限访问");
    }


添加联系人:点击按钮触发事件addContact,从而出现添加联系人界面。

/* 添加联系人 */
- (IBAction)addContact:(id)sender {
    //获得联系人信息
    NSString *name=self.nameField.text;
    NSString *phone=self.phoneField.text;
    //获得通讯录对象
    CNContactStore *contactStore=[[CNContactStorealloc]init];
    
    if ([CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts] ==CNAuthorizationStatusNotDetermined) {
        [contactStore requestAccessForEntityType:CNEntityTypeContactscompletionHandler:^(BOOL granted,NSError * _Nullable error) {
            if (granted) {
                NSLog(@"授权允许");
            }
            else
            {
                NSLog(@"拒绝访问");
            }
        }];
    }
    if ([CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts] ==CNAuthorizationStatusAuthorized) {
        NSLog(@"已授予过权限");
        //初始化单个联系人,注意一定是可变的,不可变的属性是只读的
        CNMutableContact *contact=[[CNMutableContactalloc]init];
        contact.familyName=name;
        //封装电话号码
        CNPhoneNumber *phoneNumber=[CNPhoneNumberphoneNumberWithStringValue:phone];
        CNLabeledValue *labeledValue=[[CNLabeledValuealloc]initWithLabel:@"手机号码"value:phoneNumber];
        NSArray *array=@[labeledValue];
        contact.phoneNumbers=array;
        //将请求封装成saveRequest
        CNSaveRequest *saveRequest=[[CNSaveRequestalloc]init];
        //添加单个联系人
        [saveRequest addContact:contacttoContainerWithIdentifier:nil];
        NSError *error=nil;
        //执行请求
        [contactStore executeSaveRequest:saveRequesterror:&error];
        NSLog(@"添加联系人成功");
    }
    else
    {
        UIAlertController *alertController=[UIAlertControlleralertControllerWithTitle:@"提醒"message:@"没有权限访问,请开启权限"preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action=[UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleCancelhandler:nil];
        [alertController addAction:action];
        [selfpresentViewController:alertControlleranimated:YEScompletion:nil];
        NSLog(@"没有权限访问");
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值