联系人和麦克风请求访问在iOS 9上不起作用
自上周一以来,我一直被困在一个错误中,所以我现在寻求帮助..
联系人和Micriohpone请求访问在iOS 9上不起作用。我使用这段代码来请求访问联系人:
I've been stuck on a bug since last Monday, so I'm asking for help now .. Contacts and Micriohpone request access does not work on iOS 9. I use this piece of code in order to request access to contacts :
let contactsStore = CNContactStore()
func requestAccess(completionHandler: @escaping (Permission) -> ()) {
self.contactsStore.requestAccess(for: .contacts, completionHandler: { (granted, error) in
if granted {
completionHandler(.granted)
} else {
completionHandler(.denied)
}
})
}
此函数被调用,没问题,问题是它总是返回.denied,并且即使未向用户显示警告,也设置了带有访问被拒绝的错误。麦克风也一样。
This function is called, no problem with that, the problem is it always return .denied and an error set with "Access denied", even though no alert has been shown to the user. The same with microphone.
密钥隐私-联系人使用说明出现在我的Info.plist中
The key 'Privacy - Contacts Usage Description' is present in my Info.plist
编辑:
我也知道,当用户拒绝一次使用后,就不再显示它了,但是另一个问题是,应用程序的设置部分中没有开关。我尝试还原该设备(因为我没有真正的iOS 9设备,所以正在使用模拟器),但仍然具有相同的行为。
此代码可以在iOS 10和iOS 11上正常运行。但是在iOS 9上没有机会
This code works perfeclty on iOS 10 and iOS 11. But no chance on iOS 9
如果您能在这个问题上为我提供帮助,那就太好了。
If you could help me on this issue that would be awesome.
谢谢!
我以可想象的最简单的方式在9.3上进行了尝试,但确实得到了提示:
I tried this on 9.3 in the simplest way imaginable, and I did get a prompt:
import UIKit
import Contacts
class ViewController: UIViewController {
let contactsStore = CNContactStore()
override func viewDidAppear(_ animated: Bool) {
DispatchQueue.main.async {
self.requestAccess(completionHandler: { (permission) in
print("The user said \(permission)")
})
}
}
func requestAccess(completionHandler: @escaping (Permission) -> ()) {
self.contactsStore.requestAccess(for: .contacts, completionHandler: { (granted, error) in
if granted {
completionHandler(.granted)
} else {
completionHandler(.denied)
}
})
}
}
enum Permission {
case granted
case denied
}
这很好,我认为问题是您已经拒绝了。
This works fine. I think the issue is that you already denied it.
唯一的解决方案是:
- 更改捆绑包ID,这将使您的应用程序与众不同
- 重置设备/模拟器(当然,如果有模拟器,则更容易)
- 将隐私设置从关闭更改为开
对于最终用户,我已经看到UI提示用户如果看到被拒绝,则更改设置。
For end users, I've seen the UI prompt the user to change the setting if they see "denied".
您可以这样做:
self.requestAccess(completionHandler: { (permission) in
print("The user said \(permission)")
if ( permission == .denied ) {
let urlStr = UIApplicationOpenSettingsURLString
if let url = URL(string:urlStr) {
UIApplication.shared.openURL(url)
}
}
})