在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"); }
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
