关于iOS系统中通讯录的访问

转自:http://blog.csdn.net/jasonblog/article/details/8468504


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

[cpp]  view plain copy
  1. ABAddressBookRef addressBook = ABAddressBookCreate();  

不过 在iOS 6上 ,这个API返回空值。苹果提供了如下API:

[cpp]  view plain copy
  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. // a 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要访问通讯录都应该得到用户的授权:

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

[cpp]  view plain copy
  1. // Users are able to grant or deny access to contact data on a 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, a 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);  

[cpp]  view plain copy
  1. ABAddressBookRef addressBook = NULL;  
  2. __block BOOL accessGranted = NO;  
  3.   
  4. if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6  
  5.     dispatch_semaphore_t sema = dispatch_semaphore_create(0);  
  6.     ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {  
  7.         accessGranted = granted;  
  8.         dispatch_semaphore_signal(sema);  
  9.     });  
  10.     dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);  
  11.     dispatch_release(sema);  
  12. }  
  13. else { // we're on iOS 5 or older  
  14.     accessGranted = YES;  
  15. }  
  16.   
  17. if (accessGranted) {  
  18.     // Do whatever you want here.  
  19. }  

不过这只会在第一次请求授权时才显示对话框,如果用户已经拒绝了,我们可以判断下授权状态:

[cpp]  view plain copy
  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 a 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 想访问您的日历


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

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值