美文网首页
通讯录ContactsUI

通讯录ContactsUI

作者: 董立权 | 来源:发表于2017-07-19 12:23 被阅读0次
    • AddressBookUI.framework
      提供了联系人列表界面 联系人详情界面 添加联系人界面等
      一般用于选择联系人

    • AddressBook.framework
      纯C语言的API,仅仅是获取联系人数据
      没有提供UI界面展示,需要自己搭建联系人展示界面
      里面数据类型大部分基于Core Foundation框架,使用比较复杂

    • 从iOS6开始,需要得到用户授权才能访问通讯录,因此在使用之前,需要检查用户是否已经授权

    通讯带界面的使用(AddressBookUI)

    • 导入框架,头文件
    //导入框架
    AddressBookUI.framework
    //导入头文件
    #import <AddressBookUI/AddressBookUI.h>
    
    • 请求用户授权
    //info.plist文件中添加授权
    <key>NSContactsUsageDescription</key>
        <string>允许用户访问通讯录</string>
    
        /*
         kABAuthorizationStatusNotDetermined = 0,  未授权
         kABAuthorizationStatusRestricted,         受限制
         kABAuthorizationStatusDenied,             被拒绝
         kABAuthorizationStatusAuthorized          已授权
         */
        //获取授权状态
        ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
        if(status == kABAuthorizationStatusNotDetermined) {
            //创建通讯录
            ABAddressBookRef addressBook = ABAddressBookCreate();
            //请求用户收钱
            ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
                if(error){
                    NSLog(@"%@",error);
                }
                if(granted){
                    NSLog(@"授权成功");
                }
            });
        }
    
    
    • 创建通讯录控制器
      //判断是否已授权
        if(ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
            //创建通讯录控制器
            ABPeoplePickerNavigationController *pickerNC = [[ABPeoplePickerNavigationController alloc] init];
            //设置代理 注意不要写成delegate
            pickerNC.peoplePickerDelegate = self;
            //弹出控制器
            [self presentViewController:pickerNC animated:YES completion:nil];
    
        }
    
    
    • 代理方法
    #pragma mark - ABPeoplePickerNavigationControllerDelegate
    //取消
    -(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
        
    }
    //选择联系人
    -(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person{
        
    }
    //选择联系人的某一个具体属性 如果调用了 void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person这个方法,那么该方法不会执行
    -(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
        
    }
    
    • 通讯录获取用户姓名
    //选择联系人
    //peoplePicker:控制器
    //person:用户选择的联系人
    -(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person{
        NSLog(@"选择了联系人");
        //获取联系人的姓名和电话
        //1.获取姓名
        CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
        CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
        //1.1桥接
        NSString *firstNameStr = (__bridge NSString *)(firstName);
        NSString *lastNameStr = (__bridge NSString *)(lastName);
        NSLog(@"firstNameStr:%@ lastNameStr:%@",firstNameStr,lastNameStr);
        
        //释放
        CFRelease(firstName);
        CFRelease(lastName);
        
        //2.获取电话
        ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
        //2.1获取phones的长度
        CFIndex phonesCount = ABMultiValueGetCount(phones);
        //2.1遍历
        for (NSInteger i = 0; i < phonesCount; i++) {
            NSString *label = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(phones, i));
            NSString *value = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i));
            NSLog(@"label:%@ value:%@",label,value);
        }
    }
    CFRelease(phones);
    
    • 桥接的四种方式
        //方式1
        NSString *str = (__bridge NSString *)(firstName);
        //方式2不需要管理内存
        NSString *str = CFBridgingRelease(firstName);
        //方式3移交对象的管理所有权
        NSString *str = (__bridge_transfer NSString *)firstName;
        //方式4 将Foundation框架桥接到CoreFoundation框架
        NSString *str = (__bridge_retained <#CF type#>)<#expression#>)firstName;
    

    通讯录不带界面

    • 导入框架
    //导入框架
    AddressBook.framework
    //导入头文件
    #import <AddressBook/AddressBook.h>
    
    • 请求授权(跟有界面方式一样)
    • 获取姓名和电话
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        //判断是否授权
        if(ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
            //创建通讯录
            ABAddressBookRef addressBook = ABAddressBookCreate();
            //获取所有通讯录数据
            CFArrayRef peoples = ABAddressBookCopyArrayOfAllPeople(addressBook);
            //2.选择某一个联系人的数据
            //2.1获取数组的长度
            CFIndex peopleCount = CFArrayGetCount(peoples);
            //遍历数组
            for (NSInteger i = 0; i < peopleCount; i++) {
                //获取某一个联系人
                ABRecordRef person = CFArrayGetValueAtIndex(peoples, i);
                //3.获取联系人的姓名和电话
                //姓名
                NSString *firstname = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
                NSString *lastname = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
                NSLog(@"%@,%@",firstname,lastname);
                //电话
                ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
                //获取长度
                CFIndex phoneCount = ABMultiValueGetCount(phones);
                //遍历
                for (NSInteger i = 0; i < phoneCount; i++) {
                    NSString *label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, i));
                    NSString *value = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i));
                    NSLog(@"%@,%@",label,value);
                    
                }
                CFRelease(phones);
            }
            CFRelease(peoples);
            CFRelease(addressBook);
        }
    }
    

    通讯录ContactsUI

    • 导入框架,头文件
    //导入框架
    ContactsUI.framework
    //导入头文件
    #import <ContactsUI/ContactsUI.h>
    
    • 请求授权
    //info.plist文件中添加授权
    <key>NSContactsUsageDescription</key>
        <string>允许用户访问通讯录</string>
    
    //获取用户授权状态
        CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
        
        if(status == CNAuthorizationStatusNotDetermined){
            //创建通讯录
            CNContactStore *contactStore = [[CNContactStore alloc] init];
            //请求用户授权
            [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
                if(error){
                    NSLog(@"%@",error);
                }
                if(granted){
                    NSLog(@"授权成功");
                }
            } ];
        }
    
    • 创建控制器
    //iOS 9之后,不需要授权即可拿到通讯录数据,一定要记得授权
        CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
        if(status == CNAuthorizationStatusAuthorized){
            //创建通讯录控制器
            CNContactPickerViewController *contactPickerVC = [[CNContactPickerViewController alloc] init];
            //设置代理
            contactPickerVC.delegate = self;
            //弹出控制器
            [self presentViewController:contactPickerVC animated:YES completion:nil];
        }
    
    • 实现代理方法获取数据
    #pragma mark - CNContactPickerDelegate
    //点击取消按钮时调用
    -(void)contactPickerDidCancel:(CNContactPickerViewController *)picker{
        
    }
    //选中某一个联系人时调用
    -(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
        //姓名
        NSLog(@"%@ %@",contact.givenName,contact.familyName);
        //电话
        for (CNLabeledValue *labelValue in contact.phoneNumbers) {
            CNPhoneNumber *phoneNumber = labelValue.value;
            NSLog(@"%@ %@",labelValue.label,phoneNumber.stringValue);
        }
    }
    //选中某一个练习人的某个具体属性时调用
    -(void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty{
        
    }
    

    相关文章

      网友评论

          本文标题:通讯录ContactsUI

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