IOS开发之----通讯录访问授权
来源:互联网 发布:淘宝情侣装店铺名称 编辑:程序博客网 时间:2024/06/03 17:31
from :http://blog.sina.com.cn/s/blog_71715bf801019kx7.html
在早些时候,当iOS 6还没出来,我们访问通讯录只要如下简单的代码:
- ABAddressBookRef
addressBook = ABAddressBookCreate();
不过在iOS 6上,这个API返回空值。苹果提供了如下API:
- //
Call ABAddressBookCreateWithO ptions to create an instance of AddressBook. The - //
ABAddressBookRef will initially not have access to contact data. The app must - //
then call ABAddressBookRequestAcce ssWithCompletion 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 kABOperationNotPermitted ByUserError. - AB_EXTERN
ABAddressBookRef ABAddressBookCreateWithO ptions(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 ABAddressBookRequestAcce ssWithCompletion. 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 kABOperationNotPermitted ByUserError. - //
The user will only be prompted the first time access is requested; any subsequent calls - //
to ABAddressBookCreateWithO ptions 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(^ABAddressBookRequestAcce ssCompletionHandler)( boolgranted, CFErrorRef error); - AB_EXTERN
void ABAddressBookRequestAcce ssWithCompletion(ABAddressBookRef addressBook, ABAddressBookRequestAcce ssCompletionHandler completion) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);
- ABAddressBookRef
addressBook = NULL; - __block
BOOL accessGranted = NO; -
- if
(ABAddressBookRequestAcce //ssWithCompletion != NULL) { we're on iOS 6 addressBook = ABAddressBookCreateWithO ptions(NULL, NULL); -
dispatch_semaphore_t sema = dispatch_semaphore_create(0); -
ABAddressBookRequestAcce ssWithCompletion(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. - //
- //
kABAuthorizationStatusNo tDetermined - //
The user has not yet made a choice regarding whether this app can access the data class. - //
- //
kABAuthorizationStatusRe stricted - //
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. - //
- //
kABAuthorizationStatusDe nied - //
The user explicitly denied access to the data class for this application. - //
- //
kABAuthorizationStatusAu thorized - //
This application is authorized to access the data class. - //
- typedef
CF_ENUM(CFIndex, ABAuthorizationStatus) { -
kABAuthorizationStatusNo tDetermined = 0, -
kABAuthorizationStatusRe stricted, -
kABAuthorizationStatusDe nied, -
kABAuthorizationStatusAu thorized - };
- AB_EXTERN
ABAuthorizationStatus ABAddressBookGetAuthoriz ationStatus(void) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);
通过判断授权状态,我们可以再次提醒用户进行授权。
比较糟糕的是,在中文版上,访问通讯录会出现汉化错误,XXX 想访问您的日历:
从上图中可以看到,微信还给了用户更为详细的信息,表示现在是要访问通讯录,而非日历。
于是,我在上面提到的代码中翻了几遍,都没找到设置alertView详细信息的地方,最后在SO上得到解答 ——
在设备里面还原后,它就会再次出现!
还原步棸:
设置--》通用--》还原--》还原位置与隐私
ok就可以了!
0 0
- IOS开发之----通讯录访问授权
- IOS通讯录开发<1> 授权应用访问通讯录
- iOS开发之-----iOS如何访问通讯录
- iOS开发之如何访问通讯录
- iOS开发之通讯录
- IOS8 通讯录访问授权
- IOS开发笔记 IOS如何访问通讯录
- iOS开发:通讯录之通讯录列表实现
- iOS开发之系统通讯录
- ios开发 之 通讯录操作
- iOS访问通讯录开发-读取联系人信息
- iOS访问通讯录开发-读取联系人信息
- iOS学习之——通讯录授权状态
- IOS开发笔记(12)IOS如何访问通讯录
- IOS开发笔记(12)IOS如何访问通讯录
- iOS 访问通讯录
- IOS---访问通讯录
- IOS如何访问通讯录
- 无废话MVC入门教程一[概述、环境安装、创建项目]
- 学习javacv之二:javacv0.8开发环境的搭建
- 用引用返回值(转)
- Hibernate基础知识点总结
- 错误"因为数据库正在使用,所以无法获得对数据库的独占访问权"的解决方案
- IOS开发之----通讯录访问授权
- 开源镜像站收藏
- error LNK2001: unresolved external symbol GUID_SELF_DEFINE
- 10. 泛型 Part 3 --- 学习笔记
- android中Activity的启动方式
- skipped by case
- 关于各类存储器的一些简单的认识
- Speed Limit - POJ 2017 水题
- jQuery中实现:当文本框输入值,验证值不符合规格,然后是文本框输出原来的值的方法