iOS調用系統通訊錄(適配iOS9、iOS10)(轉載)
阿新 • • 發佈:2018-01-30
tpi determine getc dispatch play bridge osi n) text2
由於系統的通訊錄在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 @interfaceViewController : 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>
iOS調用系統通訊錄(適配iOS9、iOS10)(轉載)