gpt4 book ai didi

ios - 在 Swift 3 中请求访问联系人

转载 作者:IT王子 更新时间:2023-10-29 05:33:14 25 4
gpt4 key购买 nike

我想访问用户的通讯录,并计划使用 Apple 提供的通讯录和 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,但我仍然遇到错误。错误是“实例成员‘async’不能用于‘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)
}
}

谁能帮忙解决这个问题?任何帮助将不胜感激。提前致谢。

干杯,西奥

最佳答案

代替

DispatchQueue.async(group: DispatchQueue.main, execute: { ... }

DispatchQueue.main.async { ... }

顺便说一句,如果权限之前已被拒绝,则无需再次请求授权,因为操作系统不会向最终用户显示任何“授予访问权限”的用户界面。只有当用户之前没有拒绝访问时,它才会这样做。

如果访问通讯录对于应用程序的成功运行确实必不可少,您可以向他们显示一个提醒,让他们可以选择直接从您的应用程序转到“设置”:

func requestAccess(completionHandler: @escaping (_ accessGranted: Bool) -> Void) {
switch CNContactStore.authorizationStatus(for: .contacts) {
case .authorized:
completionHandler(true)
case .denied:
showSettingsAlert(completionHandler)
case .restricted, .notDetermined:
store.requestAccess(for: .contacts) { granted, error in
if granted {
completionHandler(true)
} else {
DispatchQueue.main.async {
self.showSettingsAlert(completionHandler)
}
}
}
}
}

private func showSettingsAlert(_ completionHandler: @escaping (_ accessGranted: Bool) -> Void) {
let alert = UIAlertController(title: nil, message: "This app requires access to Contacts to proceed. Go to Settings to grant access.", preferredStyle: .alert)
if
let settings = URL(string: UIApplication.openSettingsURLString),
UIApplication.shared.canOpenURL(settings) {
alert.addAction(UIAlertAction(title: "Open Settings", style: .default) { action in
completionHandler(false)
UIApplication.shared.open(settings)
})
}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { action in
completionHandler(false)
})
present(alert, animated: true)
}

关于ios - 在 Swift 3 中请求访问联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45966955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com