美文网首页
AddressBookUI

AddressBookUI

作者: Maple_chang | 来源:发表于2016-02-19 11:11 被阅读1415次

    AddressBookUI.frameworkAddressBook.framework这2个框架在iOS2.0引入。

    AddressBookUI:

    #import <AddressBookUI/ABPeoplePickerNavigationController.h>//显示整个通讯录并可以选择一个联系人的信息
    #import <AddressBookUI/ABPersonViewController.h>//显示一个具体联系人的信息
    #import <AddressBookUI/ABNewPersonViewController.h>//增加一个新的联系人
    #import <AddressBookUI/ABUnknownPersonViewController.h>
    #import <AddressBookUI/ABAddressFormatting.h>
    

    ABPeoplePickerNavigationController这个类自带导航条,使用presentViewController从底部推出一个带导航条的联系人界面;实现ABPeoplePickerNavigationControllerDelegate代理方法.

      ABPeoplePickerNavigationController *vc = [[ABPeoplePickerNavigationController alloc] init];
        vc.peoplePickerDelegate = self;
        [self presentViewController:vc animated:YES completion:nil];
    

    ABPersonViewController查看一个联系人的详细信息:

    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, personID);
    ABPersonViewController *viewController = [[ABPersonViewController alloc] init];
    viewController.personViewDelegate = self;
    viewController.displayedPerson = person;
    viewController.allowsActions = NO;
    viewController.allowsEditing = YES;
    viewController.displayedProperties = @[[NSNumber numberWithInt:kABPersonPhoneProperty]];
    [self.navigationController pushViewController:viewController animated:YES];
    CFRelease(addressBook);
    

    ABNewPersonViewController添加新联系人

    ABNewPersonViewController *picker = [[ABNewPersonViewControlleralloc] init];
    picker.newPersonViewDelegate = self;
     
    UINavigationController *navigation = [[UINavigationControlleralloc] initWithRootViewController:picker];
    [selfpresentModalViewController:navigation animated:YES];
    

    iOS8之前有这样三个方法:
    1.- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker,用户点击联系人界面的右上角取消按钮时触发;
    2.- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person NS_DEPRECATED_IOS(2_0, 8_0),
    3.- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_DEPRECATED_IOS(2_0, 8_0),
    ios8之后新增2个方法,弃用2个方法:
    4.- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person NS_AVAILABLE_IOS(8_0),该方法会在选中某个联系人之后触发,一旦实现了这个代理方法用户只能选择到联系人视图,无法查看具体联系人的信息;// Called after a person has been selected by the user.
    5.- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0)// Called after a property has been selected by the user.
    第一个方法是选中这个人之后调用。
    第二个方法是选中这个人的详细信息后调用。


    AddressBook:

    首先创建一个ABAddressBookRef类的通讯录对象

    //创建通讯录对象 
     self.addressBook=ABAddressBookCreateWithOptions(NULL, NULL);
     //请求访问用户通讯录,注意无论成功与否block都会调用
     ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error) {
            if (!granted) {
                NSLog(@"未获得通讯录访问权限!");
            }
            [self initAllPerson];
    
        });
    

    获取通讯录中所有人

    /**
     *  取得所有通讯录记录
     */
    -(void)initAllPerson{
        //取得通讯录访问授权
        ABAuthorizationStatus authorization= ABAddressBookGetAuthorizationStatus();
        //如果未获得授权
        if (authorization!=kABAuthorizationStatusAuthorized) {
            NSLog(@"尚未获得通讯录访问授权!");
            return ;
        }
        //取得通讯录中所有人员记录
        CFArrayRef allPeople= ABAddressBookCopyArrayOfAllPeople(self.addressBook);
        self.allPerson=(__bridge NSMutableArray *)allPeople;
        //释放资源
        CFRelease(allPeople);
    

    获取通讯录中所有人员信息

    //获取所有联系人的数组
        CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(addBook);
        //获取联系人总数
        CFIndex number = ABAddressBookGetPersonCount(addBook);
        //进行遍历
        for (NSInteger i=0; i<number; i++) {
            //获取联系人对象的引用
            ABRecordRef  people = CFArrayGetValueAtIndex(allLinkPeople, i);
            //获取当前联系人名字
            NSString*firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
            //获取当前联系人姓氏
            NSString*lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));
            //获取当前联系人中间名
            NSString*middleName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNameProperty));
            //获取当前联系人的名字前缀
            NSString*prefix=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonPrefixProperty));
            //获取当前联系人的名字后缀
            NSString*suffix=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonSuffixProperty));
            //获取当前联系人的昵称
            NSString*nickName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNicknameProperty));
            //获取当前联系人的名字拼音
            NSString*firstNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonFirstNamePhoneticProperty));
            //获取当前联系人的姓氏拼音
            NSString*lastNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonLastNamePhoneticProperty));
            //获取当前联系人的中间名拼音
            NSString*middleNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNamePhoneticProperty));
            //获取当前联系人的公司
            NSString*organization=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonOrganizationProperty));
            //获取当前联系人的职位
            NSString*job=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonJobTitleProperty));
            //获取当前联系人的部门
            NSString*department=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonDepartmentProperty));
            //获取当前联系人的生日
            NSString*birthday=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonBirthdayProperty));
            NSMutableArray * emailArr = [[NSMutableArray alloc]init];
            //获取当前联系人的邮箱 注意是数组
            ABMultiValueRef emails= ABRecordCopyValue(people, kABPersonEmailProperty);
            for (NSInteger j=0; j<ABMultiValueGetCount(emails); j++) {
                [emailArr addObject:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(emails, j))];
            }
            //获取当前联系人的备注
           NSString*notes=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNoteProperty));
           //获取当前联系人的电话 数组
            NSMutableArray * phoneArr = [[NSMutableArray alloc]init];
            ABMultiValueRef phones= ABRecordCopyValue(people, kABPersonPhoneProperty);
            for (NSInteger j=0; j<ABMultiValueGetCount(phones); j++) {
                [phonerr addObject:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j))];
            }
            //获取创建当前联系人的时间 注意是NSDate
            NSDate*creatTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonCreationDateProperty));
            //获取最近修改当前联系人的时间
            NSDate*alterTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonModificationDateProperty));
            //获取地址
            ABMultiValueRef address = ABRecordCopyValue(people, kABPersonAddressProperty);
            for (int j=0; j<ABMultiValueGetCount(address); j++) {
                //地址类型
                NSString * type = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(address, j));
                NSDictionary * temDic = (__bridge NSDictionary *)(ABMultiValueCopyValueAtIndex(address, j));
                //地址字符串,可以按需求格式化
                NSString * adress = [NSString stringWithFormat:@"国家:%@\n省:%@\n市:%@\n街道:%@\n邮编:%@",[temDic valueForKey:(NSString*)kABPersonAddressCountryKey],[temDic valueForKey:(NSString*)kABPersonAddressStateKey],[temDic valueForKey:(NSString*)kABPersonAddressCityKey],[temDic valueForKey:(NSString*)kABPersonAddressStreetKey],[temDic valueForKey:(NSString*)kABPersonAddressZIPKey]];
            }
            //获取当前联系人头像图片
            NSData*userImage=(__bridge NSData*)(ABPersonCopyImageData(people));
            //获取当前联系人纪念日
            NSMutableArray * dateArr = [[NSMutableArray alloc]init];
            ABMultiValueRef dates= ABRecordCopyValue(people, kABPersonDateProperty);
            for (NSInteger j=0; j<ABMultiValueGetCount(dates); j++) {
                //获取纪念日日期
                NSDate * data =(__bridge NSDate*)(ABMultiValueCopyValueAtIndex(dates, j));
                //获取纪念日名称
                NSString * str =(__bridge NSString*)(ABMultiValueCopyLabelAtIndex(dates, j));
                NSDictionary * temDic = [NSDictionary dictionaryWithObject:data forKey:str];
                [dateArr addObject:temDic];
            }
    

    新建一个联系人

     //创建一个联系人引用
        ABRecordRef person = ABPersonCreate();
        NSString *firstName = @"哈";
        NSString *lastName = @"哈";
        // 电话号码数组
        NSArray *phones = [NSArray arrayWithObjects:@"123",@"456",nil];
        // 电话号码对应的名称
        NSArray *labels = [NSArray arrayWithObjects:@"iphone",@"home",nil];
        //这里的字段和上面的字段完全相同
        // 设置名字属性
        ABRecordSetValue(person, kABPersonFirstNameProperty,(__bridge CFStringRef)firstName, NULL);
        // 设置姓氏属性
        ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFStringRef)lastName, NULL);
        // 设置生日属性
        ABRecordSetValue(person, kABPersonBirthdayProperty,(__bridge CFDateRef)birthday, NULL);
        // 字典引用
        ABMultiValueRef dic =ABMultiValueCreateMutable(kABMultiStringPropertyType);
        // 添加电话号码与其对应的名称内容
        for (int i = 0; i < [phones count]; i ++) {
            ABMultiValueIdentifier obj = ABMultiValueAddValueAndLabel(dic,(__bridge CFStringRef)[phones objectAtIndex:i], (__bridge CFStringRef)[labels objectAtIndex:i], &obj);
        }
        // 设置phone属性
        ABRecordSetValue(person, kABPersonPhoneProperty, dic, NULL);
        // 将新建的联系人添加到通讯录中
        ABAddressBookAddRecord(addBook, person, NULL);
        // 保存通讯录数据
        ABAddressBookSave(addBook, NULL);
    

    修改联系人信息

    修改联系人的操作就是将获取和添加和在一起,先获取到相应的联系人引用,重设其属性字段即可。
    

    删除联系人

        //获取所有联系人
         NSArray *array = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addBook);
        // 遍历所有的联系人
        for (id obj in array) {
            ABRecordRef people = (__bridge ABRecordRef)obj;
            NSString *firstName = (__bridge NSString*)ABRecordCopyValue(people, kABPersonFirstNameProperty);
            NSString *lastName = (__bridge NSString*)ABRecordCopyValue(people, kABPersonLastNameProperty);
            if ([firstName isEqualToString:@"哈"] &&[lastName isEqualToString:@"哈"]) {
                ABAddressBookRemoveRecord(addBook, people,NULL);
            }
        }
        // 保存修改的通讯录对象
        ABAddressBookSave(addBook, NULL);
    

    内存管理

    -(void)dealloc{
        
        if (self.addressBook != NULL) {
            CFRelease(self.addressBook);
        }
    }
    在我们用ABAddressBookCreate()创建一个引用对象时,切记无论ARC还MRC,要用CFRelease()进行释放引用,例如上面的例子,我们需要加上这句代码
     CFRelease(self.addressBook);
    

    contactsUI

    iOS9中,系统也为我们封装好了一套联系人的UI界面,用起来也十分方便,主要新增的controller有两个:

    CNContactPickerViewController:展示联系人列表的controller
    CNContactViewController:展示联系人详细信息的controller

    示例如下:

    弹出联系人列表:
        CNContactPickerViewController * con = [[CNContactPickerViewController alloc]init];
        [self presentViewController:con animated:YES completion:nil];
    

    CNContactPickerDelegate代理方法:

    //视图取消时 调用的方法
    - (void)contactPickerDidCancel:(CNContactPickerViewController *)picker;
    //选中与取消选中时调用的方法
    - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact;
    - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;
    - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact*> *)contacts;
    - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperties:(NSArray<CNContactProperty*> *)contactProperties;
    

    CNContactViewController则是用来显示具体联系人的详细信息的

     CNContactViewController * con = [CNContactViewController viewControllerForContact:contact];
        [self presentViewController:con animated:YES completion:nil];
    //将要展示联系人信息与已经展示联系人信息的回调
    - (BOOL)contactViewController:(CNContactViewController *)viewController shouldPerformDefaultActionForContactProperty:(CNContactProperty *)property;
    - (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(nullable CNContact *)contact;
    
    

    相关文章

      网友评论

          本文标题:AddressBookUI

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