打开相机Swift时WKWebView关闭

时间:2019-02-13 16:05:56

标签: ios swift wkwebview

我有一个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)
    }
}

1 个答案:

答案 0 :(得分:0)

您应该在应用启动时要求用户授予访问摄像头的权限。很简单:

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

    }
}

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