在iOS 6中,以前工作正常的訪問(wèn)通訊錄的iPhone程序可能會(huì)出錯(cuò),現(xiàn)象是程序啟動(dòng)時(shí)不提醒用戶是否允許程序訪問(wèn)通訊錄,同時(shí)在“設(shè)置->隱私->通訊錄”中看不到你的程序。另外,對(duì)通訊錄進(jìn)行操作的代碼會(huì)報(bào)類似于以下消息的錯(cuò)誤:
?
?
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加強(qiáng)了通訊錄訪問(wèn)控制,要求開(kāi)發(fā)人員顯式聲明需要訪問(wèn)通訊錄,方法是調(diào)用
?
ABAddressBookRequestAccessWithCompletion
?
方法,具體參見(jiàn)官方文檔:
http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/index.html
?
下面是對(duì)應(yīng)的樣例代碼,一般來(lái)講需要將這段代碼放置在程序啟動(dòng)部分,在程序啟動(dòng)過(guò)程中提示用戶本程序需要訪問(wèn)設(shè)備上的通訊錄:
?
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"); }
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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