2016-07-07 55 views
8

只要它試圖請求訪問CNContactStore,我的代碼就會崩潰。任何想法,如果這是一個測試版問題?Swift 3/Xcode 8 - CNContact [access] <Private>

var addressBookStore = CNContactStore() 

addressBookStore.requestAccess(for: .contacts) { (granted, error) 

//這個控制檯消息觸發的崩潰 - 斜挎[836:1175155] [進入]私人

發生崩潰,在這條線,甚至連防止印刷錯誤!

在此先感謝

回答

21

由於這裏建議:https://developer.apple.com/reference/contacts

重要

的iOS應用鏈接上或iOS 10.0後,必須在它的Info.plist 文件的使用說明它需要的數據類型的密鑰 訪問或它會崩潰。要特別訪問聯繫人數據,它必須包含NSContactsUsageDescription 。

你必須ADDD NSContactsUsageDescription關鍵在你Info.plist文件

enter image description here

然後你會得到授權對話框。沒有這個關鍵的應用程序會崩潰

enter image description here

let addressBookStore = CNContactStore() 

addressBookStore.requestAccess(for: CNEntityType.contacts) { (isGranted, error) in 
    print(isGranted) 
    print(error) 
} 
+3

這樣一個簡單的解決方案。我不相信我錯過了!非常感謝 – InfinitePod

+1

驚人的提示! TY! –