Actions within webview - iOS

Asked

Viewed 142 times

0

good afternoon! Can anyone help me? I’m starting iOS development and I have a problem. A website on which I am creating a webview (with Mkwebview) sends alerts to the user when doing some action but in the webview I cannot make this notification appear. What can be happening? Who can help me I thank.inserir a descrição da imagem aqui

  import UIKit class ViewController: UIViewController, UIWebViewDelegate 
{ 
var webView: UIWebView! override func viewDidLoad()
 {
 super.viewDidLoad() webView = UIWebView(frame: UIScreen.main.bounds) webView.delegate = self view.addSubview(webView) if let url = URL(string: "office.unick.forex/login.php") 
{
 let request = URLRequest(url: url) webView.loadRequest(request) 
} 
}
 }
  • Hello Adriano can put your code so we can help you?

  • Can someone help me? =(

  • Somebody give me a light? :'(

1 answer

0

Analyzing your code I found that:

  • No communication between client (web page) and container (iOS).
  • The Uiwebview component was depreciated consequently, use the Wkwebview.

Thus, to display the alert correctly, your code should have a structure close to the example 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 above code, run the javascript command on the console or directly on some site function:

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

Evidence of testing:

inserir a descrição da imagem aqui

Don’t forget to add the information NSAppTransportSecurity in your plist file, otherwise your site might not be loaded.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.