咒世妖秽吧 关注:8贴子:97
  • 14回复贴,共1

iOS 6以上访问用户通讯录需要得到授权

只看楼主收藏回复


在早些时候,当iOS 6还没出来,我们访问通讯录只要如下简单的代码:
ABAddressBookRef addressBook = ABAddressBookCreate();
不过在iOS 6上,这个API返回空值。苹果提供了如下API:
// Call ABAddressBookCreateWithOptions to create an instance of AddressBook. The
// ABAddressBookRef will initially not have access to contact data. The app must
// then call ABAddressBookRequestAccessWithCompletion to request this access.
// The options argument is reserved for future use. Currently it will always be NULL.
// If access to contact data is already restricted or denied, this will fail returning
// a NULL ABAddressBookRef with error kABOperationNotPermittedByUserError.
AB_EXTERN ABAddressBookRef ABAddressBookCreateWithOptions(CFDictionaryRef options, CFErrorRef* error) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);
由于之前的iOS版本在隐私方面被人诟病,以至于出现App在没有提醒用户的情况访问通讯录而被拒绝的案例。现在每个App要访问通讯录都应该得到用户的授权:
因此,如上注释所描述的,我们应该调用如下API来获取授权:
// Users are able to grant or deny access to contact data on a per-app basis. To request
// access to contact data, call ABAddressBookRequestAccessWithCompletion. This will not
// block the app while the user is being asked to grant or deny access. Until access has
// been granted, a non-NULL ABAddressBookRef will not contain any contacts and any attempt to
// modify contacts will fail with CFErrorRef returning kABOperationNotPermittedByUserError.
// The user will only be prompted the first time access is requested; any subsequent calls
// to ABAddressBookCreateWithOptions will use the existing permissions. The completion
// handler is called on an arbitrary queue. If the ABAddressBookRef is used throughout the app,
// then all usage should be dispatched to the same queue to use ABAddressBookRef in a
// thread-safe manner.
typedef void(^ABAddressBookRequestAccessCompletionHandler)(bool granted, CFErrorRef error);
AB_EXTERN void ABAddressBookRequestAccessWithCompletion(ABAddressBookRef addressBook, ABAddressBookRequestAccessCompletionHandler completion) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);
ABAddressBookRef addressBook = NULL;
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
dispatch_release(sema);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted) {
// Do whatever you want here.
}
不过这只会在第一次请求授权时才显示对话框,如果用户已经拒绝了,我们可以判断下授权状态:
// To check the app's access to contact data. Based upon the access, the app could
// display or hide its UI elements that would access any AddressBook API.
//
// kABAuthorizationStatusNotDetermined
// The user has not yet made a choice regarding whether this app can access the data class.
//
// kABAuthorizationStatusRestricted
// This application is not authorized to access the data class. The user cannot change
// this application’s status, possibly due to active restrictions such as parental controls
// being in place.
//
// kABAuthorizationStatusDenied
// The user explicitly denied access to the data class for this application.
//
// kABAuthorizationStatusAuthorized
// This application is authorized to access the data class.
//
typedef CF_ENUM(CFIndex, ABAuthorizationStatus) {
kABAuthorizationStatusNotDetermined = 0,
kABAuthorizationStatusRestricted,
kABAuthorizationStatusDenied,
kABAuthorizationStatusAuthorized
};
AB_EXTERN ABAuthorizationStatus ABAddressBookGetAuthorizationStatus(void) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);
通过判断授权状态,我们可以再次提醒用户进行授权。


IP属地:山东1楼2013-06-28 16:36回复
    注意模拟器不需要授权,所以真机测试时这点容易疏忽。


    IP属地:山东2楼2013-06-28 16:38
    回复


      IP属地:山东来自iPhone客户端7楼2014-03-21 10:10
      回复


        IP属地:山东来自iPhone客户端9楼2014-03-21 10:14
        回复
          hjjjk


          IP属地:山东来自iPhone客户端10楼2014-03-21 15:03
          回复


            IP属地:山西来自iPhone客户端12楼2014-03-29 23:21
            收起回复


              IP属地:山东来自iPhone客户端14楼2014-04-18 11:39
              回复


                IP属地:山东来自iPhone客户端16楼2014-04-18 11:51
                回复
                  发一个链接试一下http://www.baidu.commmmm


                  IP属地:山东来自iPhone客户端17楼2014-05-15 11:09
                  回复
                    再来一个http://www.baidu.com 加一个空格旧好


                    IP属地:山东来自iPhone客户端18楼2014-05-15 11:12
                    回复


                      IP属地:山东来自iPhone客户端19楼2014-07-25 14:03
                      回复