【发布时间】:2017-08-30 17:59:54
【问题描述】:
我想访问用户的联系人并计划使用 Apple 提供的 Contacts 和 ContactsUI 框架。
不过,首先,我需要获得访问用户联系人的权限,但我在这样做时遇到了麻烦。在 Swift 2 中,可以像这样请求许可:
func requestForAccess(completionHandler: (accessGranted: Bool) -> Void) {
let authorizationStatus = CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts)
switch authorizationStatus {
case .Authorized:
completionHandler(accessGranted: true)
case .Denied, .NotDetermined:
self.contactStore.requestAccessForEntityType(CNEntityType.Contacts, completionHandler: { (access, accessError) -> Void in
if access {
completionHandler(accessGranted: access)
}
else {
if authorizationStatus == CNAuthorizationStatus.Denied {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let message = "\(accessError!.localizedDescription)\n\nPlease allow the app to access your contacts through the Settings."
self.showMessage(message)
})
}
}
})
default:
completionHandler(accessGranted: false)
}
}
我尝试像这样将其转换为 Swift 3,但仍然出现错误。错误是“Instance member 'async' cannot be used on type 'DispatchQueue';你的意思是改用这种类型的值吗?”:
func requestForAccess(completionHandler: @escaping (_ accessGranted: Bool) -> Void) {
let authorizationStatus = CNContactStore.authorizationStatus(for: CNEntityType.contacts)
switch authorizationStatus {
case .authorized:
completionHandler(true)
case .denied, .notDetermined:
self.contactStore.requestAccess(for: CNEntityType.contacts, completionHandler: { (access, accessError) -> Void in
if access {
completionHandler(access)
}
else {
if authorizationStatus == CNAuthorizationStatus.denied {
DispatchQueue.async(group: DispatchQueue.main, execute: { () -> Void in //error here
let message = "\(accessError!.localizedDescription)\n\nPlease allow the app to access your contacts through the Settings."
self.showMessage(message)
})
}
}
})
default:
completionHandler(false)
}
}
谁能帮忙解决这个问题?任何帮助将不胜感激。提前致谢。
干杯, 西奥
【问题讨论】:
-
出现了哪些错误?
-
“我仍然想出错误”不是一个问题。清楚地说明“错误”是什么以及在哪一行。另外,你真的想象在 Stack Overflow 之前没有人讨论过如何在 Swift 3 中授权联系人访问吗?在询问之前尝试搜索。节省带宽。
-
@matt 我认为堆栈溢出不需要这种攻击性。我添加了错误,但只是冷静下来
-
@GIJOW 添加在上面。 “实例成员 'async' 不能用于类型 'DispatchQueue';您的意思是使用这种类型的值来代替吗?”