因爲系統的通信錄在iOS9的時候提供了新的api,因此咱們2種框架都使用。首先咱們要導入框架:ios
/// 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]; 就能簡單的調用系統通信錄。api
若是咱們輸入一個號碼也能夠直接判斷,這個號碼是否在通信錄內,若是在則調取該用戶信息,數組
#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>