打开相机时 WKWebView 关闭 Swift

WKWebView closes when opening camera Swift

我有一个 WKWebView 应用程序,在显示的网页上有一个文件条目,它应该在其中拍照。每当我按下此输入时,应用程序都会关闭,在 android 中,我设法通过一些权限解决,但在 ios 中,我没有经验。

我能做什么?这个打开相机的按钮就在上传到 WKwebview 的网站上。

我的代码

//
//  ViewController.swift


import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate{

    @IBOutlet weak var webView: WKWebView!

        //  Inicio Atividade
    override func viewDidLoad() {
        super.viewDidLoad()

        // Oculta Navbar
        self.navigationController?.isNavigationBarHidden = true

        //  User Agent
        webView.customUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1"
        webView.navigationDelegate = self

        // Verifica Conexao
        if CheckInternet.Connection(){

            let url = URL(string: "https://www.sitemercado.com.br/frade/")
            let request = URLRequest(url: url!)
            webView.load(request)
        }
        else{
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let secondVC = storyboard.instantiateViewController(withIdentifier: "OffViewController") as! OffViewController
            self.navigationController?.pushViewController(secondVC, animated: false)
        }
    }

    // Notifi Segundo Plano
    override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    NotificationCenter.default.addObserver(self, selector: #selector(WillEnterForeground), name: .UIApplicationWillEnterForeground, object: nil)
    }

    // Segundo plano verifica
    @objc fileprivate func WillEnterForeground() {
        if CheckInternet.Connection(){
        }

        else{
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let secondVC = storyboard.instantiateViewController(withIdentifier: "OffViewController") as! OffViewController
            self.navigationController?.pushViewController(secondVC, animated: false)
        }
    }

    // Limpa Notifi
    deinit {
        NotificationCenter.default.removeObserver(self, name: .UIApplicationWillEnterForeground, object: nil)
    }
    // Detectar Erro Beta
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let secondVC = storyboard.instantiateViewController(withIdentifier: "OffViewController") as! OffViewController
        self.navigationController?.pushViewController(secondVC, animated: false)
    }

    // Tratamento target_blank
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.targetFrame == nil {
            if let url = navigationAction.request.url {
                let app = UIApplication.shared
                if app.canOpenURL(url) {
                    app.open(url, options: [:], completionHandler: nil)
                }
            }
        }
        decisionHandler(.allow)
    }
}

您应该在应用程序启动时要求用户授予相机访问权限。非常简单:

AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in
    if response {
        //access granted
    } else {

    }
}

用户授予权限后,您应该可以拍照了:)