[英]Contacts and Microphone request access does not work on iOS 9
自上周一以來,我一直被困在一個錯誤上,所以我現在尋求幫助.. 聯系人和 Micriohpone 請求訪問在 iOS 9 上不起作用。我使用這段代碼來請求訪問聯系人:
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 和一個錯誤設置為“訪問被拒絕”,即使沒有向用戶顯示警報。 麥克風也是一樣。
關鍵的“隱私 - 聯系人使用說明”存在於我的 Info.plist 中
編輯:我也知道當用戶拒絕使用它不再顯示時,但另一個問題是應用程序的設置部分沒有“開關”。 我試圖恢復設備(在模擬器上工作,因為我沒有真正的 iOS 9 設備),但仍然是相同的行為。
此代碼在 iOS 10 和 iOS 11 上完美運行。但在 iOS 9 上沒有機會
如果你能在這個問題上幫助我,那就太棒了。
謝謝 !
我以可以想象的最簡單的方式在 9.3 上嘗試了這個,我確實得到了一個提示:
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
}
這工作正常。 我認為問題是你已經否認了。
唯一的解決辦法是:
對於最終用戶,我已經看到 UI 提示用戶在看到“拒絕”時更改設置。
你可以這樣做:
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)
}
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.