导入#import <AddressBook/AddressBook.h>
请求权限
//请求权限- (void)requestAddressBook{ ABAddressBookRef bookRef = ABAddressBookCreate();; ABAddressBookRequestAccessWithCompletion(bookRef, ^(bool granted, CFErrorRef error) { if (granted) { NSLog(@"授权成功!"); [self getAllContact:bookRef]; } else { // 判断当前的授权状态是否是用户还未选择的状态 ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus(); if (status == kABAuthorizationStatusAuthorized) { }else{ NSLog(@"授权失败!==%@",@(status)); } NSLog(@"授权失败!"); } });}
获取联系人
//获取通讯录信息- (void)getAllContact:(ABAddressBookRef)bookRef{ // 获取通讯录中所有的联系人 CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeople(bookRef); // 遍历所有联系人 CFIndex count = CFArrayGetCount(arrayRef); for (int i = 0; i < count; i++) { ABRecordRef record = CFArrayGetValueAtIndex(arrayRef, i); // 获取姓名 NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty); NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty); NSLog(@"firstName = %@, lastName = %@", firstName, lastName); // 获取电话号码 ABMultiValueRef multiValue = ABRecordCopyValue(record, kABPersonPhoneProperty); CFIndex count = ABMultiValueGetCount(multiValue); for (int i = 0; i < count; i ++) { NSString *label = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(multiValue, i); NSString *phone = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(multiValue, i); NSLog(@"label = %@, phone = %@", label, phone); } CFRelease(multiValue); } CFRelease(bookRef); CFRelease(arrayRef);}