1
votes

I have a WKWebView application, there is a file entry on the webpage shown, in which it should take a photo. Whenever I press this input the application closes, in android I managed to resolve with some permissions but in ios i am inexperienced.

What can I do ? this button that opens camera is in the very site that is uploaded to WKwebview.

My code

//
//  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
Does it crash? If so which point is it?Satheesh
There is a file entry on the web page shown, where you should take a photo. Whenever I press this input the application closesdenis

1 Answers

0
votes

You should ask user to give the permission for camera access in the startup of the app. It is very easy:

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

    }
}

After user grant permission you should be able to take the picture :)