Actions within webview - iOS

0

Good afternoon! Can someone help me? I'm starting in iOS development and I'm having a problem. A website in which I'm creating a webview (with MKWebView) issues alerts to the user when I take action but in the webview I can not make that notification appear. What could be happening?

importUIKitclassViewController:UIViewController,UIWebViewDelegate{varwebView:UIWebView!overridefuncviewDidLoad(){super.viewDidLoad()webView=UIWebView(frame:UIScreen.main.bounds)webView.delegate=selfview.addSubview(webView)ifleturl=URL(string:"office.unick.forex/login.php") 
{
 let request = URLRequest(url: url) webView.loadRequest(request) 
} 
}
 }
    
asked by anonymous 11.08.2018 / 23:54

1 answer

0

Looking at your code, I found that:

  • There is no communication between the client (web page) and the container (iOS).
  • The UIWebView component has been deprecated accordingly, use the WKWebView .

In order to display the alert correctly, your code should have a structure similar to the one below:

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate, WKScriptMessageHandler {

    // Troquei de UIWebView para WKWebView
    var webView: WKWebView!

    override func viewDidLoad() {
        webView = WKWebView(frame: UIScreen.main.bounds)

        webView.configuration.userContentController.add(self, name: "alerta") //window.webkit.messageHandlers.alerta.postMessage("trigger from JS");
        webView.uiDelegate = self
        self.view.addSubview(webView)

        if let url = URL(string: "http://office.unick.forex/login.php")
        {
            let request = URLRequest(url: url)
            webView.load(request)
        }
    }

    func alertaGenerico() {
        let alertController = GlobalAlertController(title: "", message: "Você tem certeza?", preferredStyle: .alert)

        let cancelar = UIAlertAction(title: "Cancelar", style: .default, handler: nil)
        alertController.addAction(cancelar)

        let ok = UIAlertAction(title: "Ok", style: .default, handler: nil)
        alertController.addAction(ok)

        alertController.show()
    }

    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if message.name == "alerta" {
            print(message.body)
            alertaGenerico()
        }
    }
}

//--------
// MARK: - Exibição global na UIViewController
//--------
protocol GlobalWindow {
    func show(animated flag: Bool, completion: (() -> Void)? )
}

extension GlobalWindow where Self : UIViewController {

    func show(animated flag: Bool = true, completion: (() -> Void)? = nil) {
        if let window = UIApplication.shared.keyWindow?.rootViewController {
            window.present(self, animated: flag, completion: completion)
        }
    }
}

class GlobalAlertController : UIAlertController, GlobalWindow {}

To test the code above, run the javascript command on the console or directly on some site function:

window.webkit.messageHandlers.alerta.postMessage("texto de parametro");

Evidence of testing:

Do not forget to add the NSAppTransportSecurity information to your plist file, otherwise your site may not load.

    
08.09.2018 / 02:07