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(@"發(fā)生錯誤!");
}
});
}else{//非首次訪問通訊錄
NSArray *contacts = [self fetchContactWithAddressBook:addressBook];
dispatch_async(dispatch_get_main_queue(), ^{
//----------------主線程 更新 UI-----------------
NSLog(@"contacts:%@", contacts);
});
}
}
- (NSMutableArray *)fetchContactWithAddressBook:(ABAddressBookRef)addressBook{
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {////有權限訪問
//獲取聯(lián)系人數(shù)組
NSArray *array = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSMutableArray *contacts = [NSMutableArray array];
for (int i = 0; i < array.count; i++) {
//獲取聯(lián)系人
ABRecordRef people = CFArrayGetValueAtIndex((__bridge ABRecordRef)array, i);
//獲取聯(lián)系人詳細信息,如:姓名,電話,住址等信息
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{
//創(chuàng)建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(@"發(fā)生錯誤!");
}
}];
}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;
//創(chuàng)建數(shù)組,必須遵守CNKeyDescriptor協(xié)議,放入相應的字符串常量來獲取對應的聯(lián)系人信息
NSArray <id<CNKeyDescriptor>> *keysToFetch = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey];
//獲取通訊錄數(shù)組
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:這里沒有做排序, 需要自己做排序處理.