ios之通讯录 ios9和ios10

来源:互联网 发布:北京警察网络联系 编辑:程序博客网 时间:2024/06/05 10:13

由于系统的通讯录在iOS9的时候提供了新的api,所以我们2种框架都使用。首先我们要导入框架

[objc] view plain copy
  1. /// iOS 9前的框架  
  2. #import <AddressBook/AddressBook.h>  
  3. #import <AddressBookUI/AddressBookUI.h>  
  4. /// iOS 9的新框架  
  5. #import <ContactsUI/ContactsUI.h>  
[objc] view plain copy
  1. #define Is_up_Ios_9             ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0  
[objc] view plain copy
  1. @interface ViewController : UIViewController<ABPeoplePickerNavigationControllerDelegate,CNContactPickerDelegate>  


接着在需要调用通讯录的vc里面添加一下代码

[objc] view plain copy
  1. #pragma mark ---- 调用系统通讯录  
  2. - (void)JudgeAddressBookPower {  
  3.     ///获取通讯录权限,调用系统通讯录  
  4.     [self CheckAddressBookAuthorization:^(bool isAuthorized , bool isUp_ios_9) {  
  5.         if (isAuthorized) {  
  6.             [self callAddressBook:isUp_ios_9];  
  7.         }else {  
  8.             NSLog(@"请到设置>隐私>通讯录打开本应用的权限设置");  
  9.         }  
  10.     }];  
  11. }  
  12.       
  13. - (void)CheckAddressBookAuthorization:(void (^)(bool isAuthorized , bool isUp_ios_9))block {  
  14.     if (Is_up_Ios_9) {  
  15.         CNContactStore * contactStore = [[CNContactStore alloc]init];  
  16.         if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusNotDetermined) {  
  17.             [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * __nullable error) {  
  18.                 if (error)  
  19.                 {  
  20.                     NSLog(@"Error: %@", error);  
  21.                 }  
  22.                 else if (!granted)  
  23.                 {  
  24.                       
  25.                     block(NO,YES);  
  26.                 }  
  27.                 else  
  28.                 {  
  29.                     block(YES,YES);  
  30.                 }  
  31.             }];  
  32.         }  
  33.         else if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusAuthorized){  
  34.             block(YES,YES);  
  35.         }  
  36.         else {  
  37.             NSLog(@"请到设置>隐私>通讯录打开本应用的权限设置");  
  38.         }  
  39.     }else {  
  40.         ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULLNULL);  
  41.         ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();  
  42.           
  43.         if (authStatus == kABAuthorizationStatusNotDetermined)  
  44.         {  
  45.             ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {  
  46.                 dispatch_async(dispatch_get_main_queue(), ^{  
  47.                     if (error)  
  48.                     {  
  49.                         NSLog(@"Error: %@", (__bridge NSError *)error);  
  50.                     }  
  51.                     else if (!granted)  
  52.                     {  
  53.                           
  54.                         block(NO,NO);  
  55.                     }  
  56.                     else  
  57.                     {  
  58.                         block(YES,NO);  
  59.                     }  
  60.                 });  
  61.             });  
  62.         }else if (authStatus == kABAuthorizationStatusAuthorized)  
  63.         {  
  64.             block(YES,NO);  
  65.         }else {  
  66.             NSLog(@"请到设置>隐私>通讯录打开本应用的权限设置");  
  67.         }  
  68.     }  
  69. }  
  70.       
  71. - (void)callAddressBook:(BOOL)isUp_ios_9 {  
  72.     if (isUp_ios_9) {  
  73.         CNContactPickerViewController *contactPicker = [[CNContactPickerViewController alloc] init];  
  74.         contactPicker.delegate = self;  
  75.         contactPicker.displayedPropertyKeys = @[CNContactPhoneNumbersKey];  
  76.         [self presentViewController:contactPicker animated:YES completion:nil];  
  77.     }else {  
  78.         ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];  
  79.         peoplePicker.peoplePickerDelegate = self;  
  80.         [self presentViewController:peoplePicker animated:YES completion:nil];  
  81.   
  82.     }  
  83. }  
  84.   
  85. #pragma mark -- CNContactPickerDelegate  
  86. - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {  
  87.     CNPhoneNumber *phoneNumber = (CNPhoneNumber *)contactProperty.value;  
  88.     [self dismissViewControllerAnimated:YES completion:^{  
  89.         /// 联系人  
  90.         NSString *text1 = [NSString stringWithFormat:@"%@%@",contactProperty.contact.familyName,contactProperty.contact.givenName];  
  91.         /// 电话  
  92.         NSString *text2 = phoneNumber.stringValue;  
  93. //        text2 = [text2 stringByReplacingOccurrencesOfString:@"-" withString:@""];  
  94.         NSLog(@"联系人:%@, 电话:%@",text1,text2);  
  95.     }];  
  96. }  
  97.   
  98. #pragma mark -- ABPeoplePickerNavigationControllerDelegate  
  99. - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {  
  100.       
  101.     ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);  
  102.     CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef,identifier);  
  103.     CFStringRef value = ABMultiValueCopyValueAtIndex(valuesRef,index);  
  104.     CFStringRef anFullName = ABRecordCopyCompositeName(person);  
  105.       
  106.     [self dismissViewControllerAnimated:YES completion:^{  
  107.         /// 联系人  
  108.         NSString *text1 = [NSString stringWithFormat:@"%@",anFullName];  
  109.         /// 电话  
  110.         NSString *text2 = (__bridge NSString*)value;  
  111. //        text2 = [text2 stringByReplacingOccurrencesOfString:@"-" withString:@""];  
  112.         NSLog(@"联系人:%@, 电话:%@",text1,text2);  
  113.     }];  
  114. }  

最后我们可以调用 [self JudgeAddressBookPower]; 就能简单的调用系统通讯录。


tips:如果要适配iOS 10,就必须在plist文件的Source code模式下添加

[objc] view plain copy
  1. <key>NSContactsUsageDescription</key>  
  2. <string>App需要您的同意,才能访问通讯录</string>  
原创粉丝点击