一、iOS 9 以前的通讯录框架
AddressBookUI框架:提供了联系人列表界面、联系人详情界面、添加联系人界面等,一般用于选择联系人。
AddressBook框架:纯C语言的API,仅仅是获得联系人数据。没有提供UI界面展示,需要自己搭建联系人展示界面。
二、iOS 9以后最新通讯录框架
ContactsUI框架:拥有AddressBookUI框架的所有功能,使用起来更加的面向对象。
Contacts框架:拥有AddressBook框架的所有功能,不再是C语言的API,使用起来非常简单。
三、设置 info.plist 的Privacy - Contacts Usage Description
为了兼容iOS9.0前后的版本,所以在.h文件中有两个方法
@interfacePhoneContactsManager :NSObject
+ (instancetype)shareManager;
#ifdef NSFoundationVersionNumber_iOS_9_0
- (void)contactCheckedWithTarget:(nonnull UIViewController*)target handler:(void(^)(PhoneContact*contact))handler;
#endif
- (void)peopleCheckedWithTarget:(nonnull UIViewController*)target handler:(void(^)(PhoneContact*contact))handler;
@end
.m文件中引入库文件
#import
#import <AddressBookUI/AddressBookUI.h>
#ifdef NSFoundationVersionNumber_iOS_8_x_Max
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
手机权限查询
iOS 9.0之前的版本
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();
if (authStatus == kABAuthorizationStatusNotDetermined){
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error){
NSLog(@"Error: %@", (__bridge NSError *)error);
}else if (!granted){
block(NO);
}else{
block(YES);
}
});
});
}else if (authStatus == kABAuthorizationStatusAuthorized){
block(YES);
}else {
NSLog(@"请到设置>隐私>通讯录打开本应用的权限设置");
}
iOS之后的系统版本
CNContactStore * contactStore = [[CNContactStore alloc]init];
if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusNotDetermined) {
[contactStorerequestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * __nullable error) {
if (error){
NSLog(@"Error: %@", error);
}else if (!granted){
block(NO);
}else{
block(YES);
}
}];
}else if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusAuthorized){
block(YES);
}else {
NSLog(@"请到设置>隐私>通讯录打开本应用的权限设置");
}
}
方法实现
iOS9.0以后的系统版本
_ContactSelectHandler = handler;
[self checkAddressBookAuthorization:^(BOOL isAuthorized) {
if(@available(iOS 9.0, *)){
if (isAuthorized) {
CNContactPickerViewController *contactPicker = [[CNContactPickerViewController alloc] init];
contactPicker.delegate = self;
contactPicker.displayedPropertyKeys = @[CNContactGivenNameKey,CNContactFamilyNameKey, CNContactPhoneNumbersKey];
[targetpresentViewController:contactPicker animated:YES completion:nil];
}
}
}];
iOS9.0以前的系统版本
- (void)peopleCheckedWithTarget:(UIViewController *)target handler:(void (^)(PhoneContact * _Nonnull))handler{
_ContactSelectHandler = handler;
[self checkAddressBookAuthorization:^(BOOL isAuthorized) {
if (isAuthorized) {
ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;
[targetpresentViewController:peoplePicker animated:YES completion:nil];
}
}];
}
联系人信息选择通过代理方法处理
iOS9.0以后的系统版本
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty API_AVAILABLE(ios(9.0)){
CNPhoneNumber *phoneNumber = (CNPhoneNumber *)contactProperty.value;
NSString *name = [CNContactFormatter stringFromContact:contactProperty.contact style:CNContactFormatterStyleFullName];
if(_ContactSelectHandler){
PhoneContact *people = [PhoneContact new];
people.contactName = name;
people.contactPhoneNumber = phoneNumber.stringValue;
_ContactSelectHandler(people);
}
}
iOS9.0以后的系统版本
ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef,identifier);
CFStringRef value = ABMultiValueCopyValueAtIndex(valuesRef,index);
CFStringRef anFullName = ABRecordCopyCompositeName(person);
if(_ContactSelectHandler){
PhoneContact *people = [PhoneContact new];
people.contactName = (__bridge NSString * _Nonnull)(anFullName);
people.contactPhoneNumber = (__bridge NSString * _Nonnull)(value);
_ContactSelectHandler(people);
}
具体实现代码可以参考:PhoneContactManager
网友评论