1. iOS 9.0之前获取通讯录的方法
- (void)fetchAddressBookBeforeIOS9{
ABAddressBookRef addressBook = ABAddressBookCreate();
//首次访问需用户授权
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {//首次访问通讯录
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, 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}];
}
return contacts;
}else{//无权限访问
NSLog(@"无权限访问通讯录");
return nil;
}
}
打印结果:
iOS 9.0之前</br>
2. iOS 9.0 及 iOS 9.0之后获取通讯录的方法
- (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 <id<CNKeyDescriptor>> *keysToFetch = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey];
//获取通讯录数组
NSArray<CNContact*> *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 = ((CNPhoneNumber *)(contact.phoneNumbers.lastObject.value)).stringValue;
[contacts addObject:@{@"name": [givenName stringByAppendingString:familyName], @"phoneNumber": phoneNumber}];
}
return contacts;
}else {
return nil;
}
}else{//无权限访问
NSLog(@"无权限访问通讯录");
return nil;
}
}
打印结果:
iOS 9.0 及之后</br>
PS:这里没有做排序, 需要自己做排序处理.
网友评论