系统通讯录的使用之基于 AddressBook.framework 和 AddressBookUI.framework 框架的实现

通过 ABPeoplePickerNavigationController 直接调取系统通讯录,只需要通过代理方法处理相应的需求即可。
ABPeoplePickerNavigationControllerDelegate 代理方法:

//iOS8之后
//列表页选中联系人立即返回
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person NS_AVAILABLE_IOS(8_0);
//联系人详情页选中联系人具体属性后返回
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0);
//点击调起页面的“取消”按钮时调用
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;

//iOS8之前
//列表页选中联系人立即返回
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person NS_DEPRECATED_IOS(2_0, 8_0);
//联系人详情页选中联系人具体属性后返回
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_DEPRECATED_IOS(2_0, 8_0);

经典的通讯录使用中,还会涉及到模糊搜索的功能,这就需要获取用户通讯录中所有联系人的全部信息。
AddressBook.framework下获取所有联系人的信息相对简单:

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(ABAddressBookCreateWithOptions(NULL, NULL));

通过遍历,可以获取所有与输入信息相匹配的信息,将这部分信息存储起来,就可以做出模糊搜索的功能。

for (int i = 0; i < ABAddressBookGetPersonCount(addressBook); i++) {
    ABRecordRef people     = CFArrayGetValueAtIndex(allPeople, i);
    ABMultiValueRef phones = ABRecordCopyValue(people, kABPersonPhoneProperty);
    
    for (NSInteger j=0; j

访问权限:

addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
    if (!granted) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"尚未获取访问权限" message:@"您未允许PeoploLink访问您的通讯录,请到设置中开启访问通讯录的权限" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction *
_Nonnull action) {
            
        }];
        [alertC addAction:sureAction];
        [self presentViewController:alertC animated:YES completion:nil];
        
    }else {
        allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
    }
});

AddressBookUI.framework框架中,调起各种页面的头文件

#import //显示整个通讯录并可以选择一个联系人的信息
#import //显示一个具体联系人的信息
#import //增加一个新的联系人
#import //未知联系人
#import //地址格式化

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

- (void)checkPeople {
    NSArray *array = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
    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:@"孙"]) {
            ABRecordID peopleID  = ABRecordGetRecordID(people);
            ABRecordRef person   = ABAddressBookGetPersonWithRecordID(addressBook, peopleID);
            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];
        }
    }
}

ABNewPersonViewController添加新联系人页面:

//添加联系人页面调起 -- 点击“完成”按钮之后,系统会自动保存联系人信息
- (void)addPeople {
    ABNewPersonViewController *picker  = [[ABNewPersonViewController alloc] init];
    picker.newPersonViewDelegate       = self;
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:picker];
    [self presentViewController:navigation animated:YES completion:nil];
}

//创建联系人

- (void)creatPeople {
    ABRecordRef person  = ABPersonCreate();
    NSString *firstName = @"悟空";
    NSString *lastName  = @"孙";
    
    NSArray *phones = [NSArray arrayWithObjects:@"15101031039",nil];// 电话号码数组
    NSArray *labels = [NSArray arrayWithObjects:@"iPhone",nil];// 电话号码对应的名称
    ABRecordSetValue(person, kABPersonFirstNameProperty,(__bridge CFStringRef)firstName, NULL);
    ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFStringRef)lastName, 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);
    }
    ABRecordSetValue(person, kABPersonPhoneProperty, dic, NULL);
    ABAddressBookAddRecord(addressBook, person, NULL);
    ABAddressBookSave(addressBook, NULL);
}

//删除联系人

- (void)deletePeople {
    NSArray *array = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
    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(addressBook, people,NULL);
        }
    }

    ABAddressBookSave(addressBook, NULL);
}

你可能感兴趣的:(系统通讯录的使用之基于 AddressBook.framework 和 AddressBookUI.framework 框架的实现)