由于系统的通讯录在iOS9的时候提供了新的api,所以我们2种框架都使用。首先我们要导入框架:
/// iOS 9前的框架 #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> /// iOS 9的新框架 #import <ContactsUI/ContactsUI.h> #define Is_up_Ios_9 ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0 @interface ViewController : UIViewController<ABPeoplePickerNavigationControllerDelegate,CNContactPickerDelegate>
接着在需要调用通讯录的vc里面添加一下代码
#pragma mark ---- 调用系统通讯录 - (void)JudgeAddressBookPower { ///获取通讯录权限,调用系统通讯录 [self CheckAddressBookAuthorization:^(bool isAuthorized , bool isUp_ios_9) { if (isAuthorized) { [self callAddressBook:isUp_ios_9]; }else { NSLog(@"请到设置>隐私>通讯录打开本应用的权限设置"); } }]; } - (void)CheckAddressBookAuthorization:(void (^)(bool isAuthorized , bool isUp_ios_9))block { if (Is_up_Ios_9) { CNContactStore * contactStore = [[CNContactStore alloc]init]; if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusNotDetermined) { [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * __nullable error) { if (error) { NSLog(@"Error: %@", error); } else if (!granted) { block(NO,YES); } else { block(YES,YES); } }]; } else if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusAuthorized){ block(YES,YES); } else { NSLog(@"请到设置>隐私>通讯录打开本应用的权限设置"); } }else { 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,NO); } else { block(YES,NO); } }); }); }else if (authStatus == kABAuthorizationStatusAuthorized) { block(YES,NO); }else { NSLog(@"请到设置>隐私>通讯录打开本应用的权限设置"); } } } - (void)callAddressBook:(BOOL)isUp_ios_9 { if (isUp_ios_9) { CNContactPickerViewController *contactPicker = [[CNContactPickerViewController alloc] init]; contactPicker.delegate = self; contactPicker.displayedPropertyKeys = @[CNContactPhoneNumbersKey]; [self presentViewController:contactPicker animated:YES completion:nil]; } else { ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init]; peoplePicker.peoplePickerDelegate = self; [self presentViewController:peoplePicker animated:YES completion:nil]; } } #pragma mark -- CNContactPickerDelegate 进入系统通讯录页面 -- - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty { CNPhoneNumber *phoneNumber = (CNPhoneNumber *)contactProperty.value; [self dismissViewControllerAnimated:YES completion:^{ /// 联系人 NSString *text1 = [NSString stringWithFormat:@"%@%@",contactProperty.contact.familyName,contactProperty.contact.givenName]; /// 电话 NSString *text2 = phoneNumber.stringValue; //text2 = [text2 stringByReplacingOccurrencesOfString:@"-" withString:@""]; NSLog(@"联系人:%@, 电话:%@",text1,text2); }]; } #pragma mark -- ABPeoplePickerNavigationControllerDelegate 进入系统通讯录页面 -- - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty); CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef,identifier); CFStringRef value = ABMultiValueCopyValueAtIndex(valuesRef,index); CFStringRef anFullName = ABRecordCopyCompositeName(person); [self dismissViewControllerAnimated:YES completion:^{ /// 联系人 NSString *text1 = [NSString stringWithFormat:@"%@",anFullName]; /// 电话 NSString *text2 = (__bridge NSString*)value; //text2 = [text2 stringByReplacingOccurrencesOfString:@"-" withString:@""]; NSLog(@"联系人:%@, 电话:%@",text1,text2); }];
}
最后我们可以调用 [self JudgeAddressBookPower]; 就能简单的调用系统通讯录。
如果我们输入一个号码也可以直接判断,这个号码是否在通讯录内,如果在则调取该用户信息,
#pragma mark -- #pragma mark -- 根据手机号查询手机通讯录 -- - (NSString *)getNameBytel:(NSString *)telstr { telstr = [telstr stringByReplacingOccurrencesOfString:@" " withString:@""]; NSMutableArray* personArray = [[NSMutableArray alloc] init]; //打开电话本数据库 ABAddressBookRef addressRef=ABAddressBookCreate(); NSString *firstName, *lastName, *fullName; //返回所有联系人到一个数组中 personArray = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressRef); //返回联系人数量 // CFIndex personCount = ABAddressBookGetPersonCount(addressRef); for (id person in personArray) { firstName = (__bridge NSString *)ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonFirstNameProperty); firstName = [firstName stringByAppendingFormat:@" "]; lastName = (__bridge NSString *)ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonLastNameProperty); if (lastName !=nil) { fullName = [firstName stringByAppendingFormat:@"%@",lastName]; } else { fullName = firstName; } NSLog(@"联系人===%@",fullName); ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty); for(int i = 0 ;i < ABMultiValueGetCount(phones); i++) { NSString *phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phones, i); phone = [phone stringByReplacingOccurrencesOfString:@"(" withString:@""]; phone = [phone stringByReplacingOccurrencesOfString:@")" withString:@""]; phone = [phone stringByReplacingOccurrencesOfString:@"-" withString:@""]; phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"===%@",phone); if ([phone isEqualToString:telstr]) { _isNo = NO; NSArray *array = [fullName componentsSeparatedByString:@" "]; fullName = [NSString stringWithFormat:@"%@%@",array[1],array[0]]; _moren = fullName; return fullName; } else { _isNo = YES; } } } if (_isNo == YES) { _moren = @"非通讯录好友"; return @"非通讯录好友"; } return nil; }
tips:如果要适配iOS 10,就必须在plist文件的Source code模式下添加
<key>NSContactsUsageDescription</key> <string>App需要您的同意,才能访问通讯录</string>