How do I prevent a WKWebView from presenting the Camera modal if a user has denied access to the camera?

我只是一个虾纸丫 提交于 2019-11-29 15:36:02

Because you mentioned __CRASHING_DUE_TO_PRIVACY_VIOLATION__ the app is probably actually crashing because you haven't added the iOS10 permission description for using the microphone. The camera view will trigger this additional microphone permissions popup if you choose Video. In iOS10, you must fill in the description that is presented to the user or the app will crash as you have reported. In iOS9, it just presents the generic permissions popup.

Try adding a description for the key NSMicrophoneUsageDescription in your Info.plist file. In Xcode this is called Privacy - Microphone Usage Description

This other answer will give you more details of the various privacy keys and their descriptions.

It'd appear to be a bug since it only crashes when Video is selected. I found a workaround by injecting JS:

  • Set the content type to only allow photos. (sample code included below)
  • Delete/Hide the input-file
  • Disable the input-file
func requestCamera() {
    AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { response in
        if response {
            self.injectJavascript()
        } else {
            self.webView = WKWebView(frame: .zero)
        }


        self.setupRequest()
    }
}

func injectJavascript() {
    let webConfiguration = WKWebViewConfiguration()
    let contentController = WKUserContentController()
    let js = "var fileInput = document.getElementById('allMedia'); fileInput.setAttribute('accept', 'image/*');"
    let userScript = WKUserScript(source: js, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: false)
    contentController.addUserScript(userScript)
    webConfiguration.userContentController = contentController
    webView = WKWebView(frame: .zero, configuration: webConfiguration)
}

func setupRequest() {
    let url = URL(string: "...")!
    let request = URLRequest(url: url)
    webView.load(request)
}

Flow is as follows:

  1. Camera access allowed ?
    • Yes
      1. Load WKWebView normally
    • No
      1. Inject JS
      2. Setup WKWebView
      3. Load WKWebView's request
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!