ios9.0 获取通讯录数据“干货”分享

方法总结:

在iOS9.0 之前,获取通讯录中的数据大多用的都是用基于C语言的有界面[AddressBookUI.framework]或者无UI界面[AddressBook.framework]类库,在OC开发中,可能会感觉到不爽! iOS9.0 之后,苹果进行了代码封装, 非常好用,下面我们一起来体验一下吧!

细节提醒:

有过这方面开发经验的小伙伴们可能知道,若要获取通讯录数据,必须要进行用户授权, 否则App是上线不成功的。
首先对比一下授权时的代码变化(这里就以有界面类库的为例):

  • [AddressBookUI.framework] 实现

ABAddressBookRef bookref = ABAddressBookCreateWithOptions(NULL, NULL);
//用户的授权
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if (status == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(bookref, ^(bool granted, CFErrorRef error) {
if (error) {
NSLog(@"授权错误");
}
if (granted) {
NSLog(@"授权错误");
}
});
}

  • [ContactsUI/ContactsUI.h] 实现

// 注意:别忘记导入头文件
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];

if (status == CNAuthorizationStatusNotDetermined) {
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (error) {
NSLog(@"授权失败");
}else {
NSLog(@"成功授权");
}
}];
}

具体实现:

  • [AddressBookUI.framework] 实现
    • 创建控制器,并设置代理。

ABPeoplePickerNavigationController *peosonVC = [[ABPeoplePickerNavigationController alloc] init];

peosonVC.peoplePickerDelegate = self;

[self presentViewController:peosonVC animated:YES completion:nil];

  • 获取个人数据为例 调用ABPeoplePickerNavigationController代理方法的实现:

  • (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person {
    // 获取姓名
    NSString *stringName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
    NSString *secondedName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
    NSLog(@"%@,%@",stringName,secondedName);

    // 获取电话号码
    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFIndex count = ABMultiValueGetCount(phones);
    for (int i = 0; i < count; i++) {
    CFStringRef label = ABMultiValueCopyLabelAtIndex(phones, i);
    CFStringRef value = ABMultiValueCopyValueAtIndex(phones, i);
    NSString *labelPhone = CFBridgingRelease(label);
    NSString *valuePhone = CFBridgingRelease(value);
    NSLog(@"%@,%@",labelPhone,valuePhone);
    }
    }

  • 实现
    • 创建控制器,并设置代理


CNContactPickerViewController *pickerVC = [[CNContactPickerViewController alloc] init];
pickerVC.delegate = self;
[self presentViewController:pickerVC animated:YES completion:nil];

  • 创建控制器,并设置代理获取个人数据为例 调用ABPeoplePickerNavigationController代理方法的实现

    // 当选择一个联系人的时候调用
    -(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
    NSLog(@"%@",contact.givenName);
    NSLog(@"%@",contact.phoneNumbers);
    for (CNLabeledValue *labelValue in contact.phoneNumbers) {
    CNPhoneNumber *number = labelValue.value;
    NSLog(@"%@",number.stringValue);
    }
    }

对比总结:

从上面可以看出,新方法在用户授授权方面,没有太大的简化,但在代理方法获取用户的数据中,却简化了不少,获取用户数据也非常方便。

你可能感兴趣的:(ios9.0 获取通讯录数据“干货”分享)