#import "GetContactVC.h"
#import
#import
@interfaceGetContactVC ()
{
UITableView*infoTable;
NSMutableArray*dataArray;
NSMutableDictionary*dataSectionData;
NSMutableArray*allKeysArray;
NSMutableArray*sortkeysArray;/**
* 创建CNContactStore对象,用与获取和保存通讯录信息*/CNContactStore*contactStore;
}@end
@implementationGetContactVC- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor yellowColor];if (@available(iOS 9.0, *)) {
[self fetchAddressBookOnIOS9AndLater];
}else{
[self fetchAddressBookBeforeIOS9];
}
}- (void)fetchAddressBookOnIOS9AndLater{//创建CNContactStore对象
CNContactStore *contactStore =[[CNContactStore alloc] init];//首次访问需用户授权
if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusNotDetermined) {//首次访问通讯录
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError *_Nullable error) {if (!error){if (granted) {//允许
NSLog(@"已授权访问通讯录");
NSArray*contacts = [self fetchContactWithContactStore:contactStore];//访问通讯录
dispatch_async(dispatch_get_main_queue(), ^{//----------------主线程 更新 UI-----------------
NSLog(@"contacts:%@", contacts);
});
}else{//拒绝
NSLog(@"拒绝访问通讯录");
}
}else{
NSLog(@"发生错误!");
}
}];
}else{//非首次访问通讯录
NSArray *contacts = [self fetchContactWithContactStore:contactStore];//访问通讯录
dispatch_async(dispatch_get_main_queue(), ^{//----------------主线程 更新 UI-----------------
NSLog(@"contacts:%@", contacts);
});
}
}- (NSMutableArray *)fetchContactWithContactStore:(CNContactStore *)contactStore{//判断访问权限
if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusAuthorized) {//有权限访问
NSError *error =nil;//创建数组,必须遵守CNKeyDescriptor协议,放入相应的字符串常量来获取对应的联系人信息
NSArray > *keysToFetch =@[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey];//获取通讯录数组
NSArray *arr = [contactStore unifiedContactsMatchingPredicate:nil keysToFetch:keysToFetch error:&error];if (!error) {
NSMutableArray*contacts =[NSMutableArray array];for (int i = 0; i < arr.count; i++) {
CNContact*contact =arr[i];
NSString*givenName =contact.givenName;
NSString*familyName =contact.familyName;
NSString*phoneNumber = @"NULL";if ((CNPhoneNumber *)(contact.phoneNumbers.lastObject.value)) {
phoneNumber= ((CNPhoneNumber *)(contact.phoneNumbers.lastObject.value)).stringValue;
}
[contacts addObject:@{@"name": [givenName stringByAppendingString:familyName], @"phoneNumber": phoneNumber}];
}returncontacts;
}else{returnnil;
}
}else{//无权限访问
NSLog(@"无权限访问通讯录");returnnil;
}
}- (void)fetchAddressBookBeforeIOS9{
ABAddressBookRef addressBook=ABAddressBookCreate();//首次访问需用户授权
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {//首次访问通讯录
ABAddressBookRequestAccessWithCompletion(addressBook, ^(boolgranted, CFErrorRef error) {if (!error) {if (granted) {//允许
NSLog(@"已授权访问通讯录");
NSArray*contacts =[self fetchContactWithAddressBook:addressBook];
dispatch_async(dispatch_get_main_queue(),^{//----------------主线程 更新 UI-----------------
NSLog(@"contacts:%@", contacts);
});
}else{//拒绝
NSLog(@"拒绝访问通讯录");
}
}else{
NSLog(@"发生错误!");
}
});
}else{//非首次访问通讯录
NSArray *contacts =[self fetchContactWithAddressBook:addressBook];
dispatch_async(dispatch_get_main_queue(),^{//----------------主线程 更新 UI-----------------
NSLog(@"contacts:%@", contacts);
});
}
}- (NSMutableArray *)fetchContactWithAddressBook:(ABAddressBookRef)addressBook{if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {有权限访问
//获取联系人数组
NSArray *array = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSMutableArray*contacts =[NSMutableArray array];for (int i = 0; i < array.count; i++) {//获取联系人
ABRecordRef people =CFArrayGetValueAtIndex((__bridge ABRecordRef)array, i);//获取联系人详细信息,如:姓名,电话,住址等信息
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(people, kABPersonFirstNameProperty);
NSString*lastName = (__bridge NSString *)ABRecordCopyValue(people, kABPersonLastNameProperty);
ABMutableMultiValueRef*phoneNumRef =ABRecordCopyValue(people, kABPersonPhoneProperty);
NSString*phoneNumber = ((__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phoneNumRef)).lastObject;
[contacts addObject:@{@"name": [firstName stringByAppendingString:lastName], @"phoneNumber": phoneNumber}];
}returncontacts;
}else{//无权限访问
NSLog(@"无权限访问通讯录");returnnil;
}
}- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];//Dispose of any resources that can be recreated.
}