【发布时间】:2016-10-24 13:14:50
【问题描述】:
我正在尝试访问联系人框架,以便将新联系人保存到用户通讯录中。我有以下代码...
import Contacts
import ContactsUI
在文件的开头并包含一个方法...
func requestForAccess(completion: (granted: Bool) -> ()) {
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_BACKGROUND.rawValue), 0), {
let authStatus = CNContactStore.authorizationStatusForEntityType(.Contacts)
switch authStatus {
case .Authorized:
completion(granted: true)
case .Denied, .NotDetermined:
// CRASH HERE before the completion block is called, after calling
// requestAccessForEntityType
self.contactStore.requestAccessForEntityType(.Contacts, completionHandler: { (access, accessError) -> () in
if access {
completion(granted: access)
} else {
if authStatus == .Denied {
dispatch_async(dispatch_get_main_queue(), { () -> () in
let msg = "\(accessError!.localizedDescription)\n\nPlease allow the app to access your contacts through the Settings."
//self.alertService.showAlert(msg)
})
}
}
})
default:
completion(granted: false)
}
})
}
在标记的行,有一个SIABRT 崩溃。结束堆栈并运行po $arg1 会抛出以下...
error: Couldn't materialize: couldn't read the value of register x0
Errored out in Execute, couldn't PrepareToExecuteJITExpression
我在根dict 的Info.plist 中有必要的行
<key>NSContactsUsageDescription</key>
<string>We need to access Contacts to import entries direct from the app.</string>
我还在目标属性的“链接框架和库”中包含Contacts.framework 和ContactsUI.framework(常规)
【问题讨论】:
-
您的设备是否运行 iOS 10?清理项目、从设备中删除应用程序或重新启动有时会有所帮助
-
CNContactStore 框架是否在构建阶段根据需要添加为可选?你为什么要在后台线程中访问请求?
-
无关但请注意,当当前授权被“拒绝”时,没有理由致电
requestAccessForEntityType。不会再次提示用户。对requestAccessForEntityType的调用将简单地再次返回“拒绝”而不询问用户。相反,您应该显示一条警报,告知用户访问当前被拒绝,并提供启动应用程序设置页面的机会,用户可以在其中启用访问权限。 -
抱歉,我在考虑 Contacts.framework。这是发生在设备上还是模拟器上?