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

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


不过在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
        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);


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

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


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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值