码迷,mamicode.com
首页 > 其他好文 > 详细

基础功能访问

时间:2015-04-03 10:46:44      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:


<1>、通讯录访问
在iOS 6中,以前工作正常的访问通讯录的iPhone程序可能会出错,现象是程序启动时不提醒用户是否允许程序访问通讯录,同时在“设置->隐私->通讯录”中看不到你的程序。另外,对通讯录进行操作的代码会报类似于以下消息的错误:

[plain] view plaincopy

Could not compile statement for query (ABCCopyArrayOfAllInstancesOfClassInSourceMatchingProperties):  
SELECT ROWID, Name, ExternalIdentifier, Type, ConstraintsPath, ExternalModificationTag, ExternalSyncTag, AccountID, Enabled, SyncData, MeIdentifier, Capabilities FROM ABStore WHERE Enabled = ?;  

其原因是iOS 6加强了通讯录访问控制,要求开发人员显式声明需要访问通讯录,方法是调用 ABAddressBookRequestAccessWithCompletion
方法,具体参见官方文档:

http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/index.html

下面是对应的样例代码,一般来讲需要将这段代码放置在程序启动部分,在程序启动过程中提示用户本程序需要访问设备上的通讯录:

[plain] view plaincopy

ABAddressBookRef addressBook = ABAddressBookCreate();  
      
        __block BOOL accessGranted = NO;  
    if (ABAddressBookRequestAccessWithCompletion != NULL) {  
          
        // we‘re on iOS 6  
        NSLog(@"on iOS 6 or later, trying to grant access permission");  
          
        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  
          
        NSLog(@"on iOS 5 or older, it is OK");  
        accessGranted = YES;  
    }  
      
    if (accessGranted) {  
          
        NSLog(@"we got the access right");  
    } 

 

基础功能访问

标签:

原文地址:http://www.cnblogs.com/whuishine/p/3451578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!