你好,游客 登录 注册 搜索
背景:
阅读新闻

iOS6 中如何获得通讯录访问权限

[日期:2013-03-24] 来源:Linux社区  作者:keyboardota [字体: ]

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

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

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

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");
    }

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款