分享

(1)IOS开发之通讯录权限

 wintelsui 2014-12-18

 在早些时候,当iOS 6还没出来,我们访问通讯录只要如下简单的代码:

  1. ABAddressBookRef addressBook ABAddressBookCreate();  

不过在iOS 6上,这个API返回空值。苹果提供了如下API:
  1. // Call ABAddressBookCreateWithOptions to create an instance of AddressBook. The  
  2. // ABAddressBookRef will initially not have access to contact data. The app must  
  3. // then call ABAddressBookRequestAccessWithCompletion to request this access.  
  4. // The options argument is reserved for future use. Currently it will always be NULL.  
  5. // If access to contact data is already restricted or denied, this will fail returning  
  6. // NULL ABAddressBookRef with error kABOperationNotPermittedByUserError.  
  7. AB_EXTERN ABAddressBookRef ABAddressBookCreateWithOptions(CFDictionaryRef options, CFErrorRef* error) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);  

 

由于之前的iOS 版本在隐私方面被人诟病,以至于出现App在没有提醒用户的情况访问通讯录而被拒绝的案例。现在每个App要访问通讯录都应该得到用户的授权:

IOS开发之----通讯录访问授权

因此,如上注释所描述的,我们应该调用如下API来获取授权:

  

  1. // Users are able to grant or deny access to contact data on per-app basis. To request  
  2. // access to contact data, call ABAddressBookRequestAccessWithCompletion. This will not  
  3. // block the app while the user is being asked to grant or deny access. Until access has  
  4. // been granted, non-NULL ABAddressBookRef will not contain any contacts and any attempt to  
  5. // modify contacts will fail with CFErrorRef returning kABOperationNotPermittedByUserError.  
  6. // The user will only be prompted the first time access is requested; any subsequent calls  
  7. // to ABAddressBookCreateWithOptions will use the existing permissions. The completion  
  8. // handler is called on an arbitrary queue. If the ABAddressBookRef is used throughout the app,  
  9. // then all usage should be dispatched to the same queue to use ABAddressBookRef in a  
  10. // thread-safe manner.  
  11. typedef void(^ABAddressBookRequestAccessCompletionHandler)(bool granted, CFErrorRef error);  
  12. AB_EXTERN void ABAddressBookRequestAccessWithCompletion(ABAddressBookRef addressBook,  ABAddressBookRequestAccessCompletionHandler completion) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);  
  1. ABAddressBookRef addressBook NULL;  
  2. __block BOOL accessGranted NO;  
  3.   
  4. if (ABAddressBookRequestAccessWithCompletion != NULL) // we're on iOS 6  
  5.   addressBook ABAddressBookCreateWithOptions(NULL, NULL); 

  6.     dispatch_semaphore_t sema dispatch_semaphore_create(0);  
  7.     ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)  
  8.         accessGranted granted;  
  9.         dispatch_semaphore_signal(sema);  
  10.     });  
  11.     dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);  
  12.     dispatch_release(sema);  
  13.  
  14. else // we're on iOS or older  
  15.     accessGranted YES;  
  16.  
  17.   
  18. if (accessGranted)  
  19.     // Do whatever you want here.  
  20.  

不过这只会在第一次请求授权时才显示对话框,如果用户已经拒绝了,我们可以判断下授权状态:
  1. // To check the app's access to contact data. Based upon the access, the app could  
  2. // display or hide its UI elements that would access any AddressBook API.  
  3. //  
  4. // kABAuthorizationStatusNotDetermined  
  5. // The user has not yet made choice regarding whether this app can access the data class.  
  6. //  
  7. // kABAuthorizationStatusRestricted  
  8. // This application is not authorized to access the data class. The user cannot change  
  9. // this application’s status, possibly due to active restrictions such as parental controls  
  10. // being in place.  
  11. //  
  12. // kABAuthorizationStatusDenied  
  13. // The user explicitly denied access to the data class for this application.  
  14. //  
  15. // kABAuthorizationStatusAuthorized  
  16. // This application is authorized to access the data class.  
  17. //  
  18. typedef CF_ENUM(CFIndex, ABAuthorizationStatus)  
  19.     kABAuthorizationStatusNotDetermined 0,  
  20.     kABAuthorizationStatusRestricted,  
  21.     kABAuthorizationStatusDenied,  
  22.     kABAuthorizationStatusAuthorized  
  23. };  
  24. AB_EXTERN ABAuthorizationStatus ABAddressBookGetAuthorizationStatus(void__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);  

通过判断授权状态,我们可以再次提醒用户进行授权。

 

比较糟糕的是,在中文版上,访问通讯录会出现汉化错误,XXX 想访问您的日历


IOS开发之----通讯录访问授权

从上图中可以看到,微信还给了用户更为详细的信息,表示现在是要访问通讯录,而非日历。

于是,我在上面提到的代码中翻了几遍,都没找到设置alertView详细信息的地方,最后在SO上得到解答 —— 通过在plist设置NSContactsUsageDescription关键字。详细文档见此

调试经验分享:一般程序安装后,第一次会提示是否访问XXXX,再到二次的时候不管怎么它都不会提示,这样就不方便大家的调试了!
在设备里面还原后,它就会再次出现!
还原步棸:
设置--》通用--》还原--》还原位置与隐私
ok就可以了!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多