Swift javascript call from UIWebView

I am trying to make a call from a javascript function in a UIWebView for Swift in iOS 10. I have a very simple project to try to get this to work, the code is below.

import UIKit class ViewController: UIViewController, UIWebViewDelegate { @IBOutlet var webView: UIWebView! override func viewDidLoad() { super.viewDidLoad() let url = Bundle.main.url(forResource: "products", withExtension: "html") let request = NSURLRequest(url: url! as URL) webView.loadRequest(request as URLRequest) } @IBAction func closeDocumentViewer() { displayView.isHidden = true; } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 

If I am the only one to get a string from a javascript function, what do I need to add to the above?

+5
source share
2 answers

A custom URL Scheme , such as myawesomeapp , should be configured and intercept requests using:

 func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool 

Leave the native code call with window.location=myawesomeapp://hello=world and get the query parameters that you pass from request.URL.query to your own code.

For more information, see my UIWebView question here: JavaScript-synchronous native communication with WKWebView

+1
source

I would suggest learning WKWebView instead of UIWebView . You will not need to register a custom URL scheme. In addition, UIWebView is deprecated, and WKWebView has many advantages, primarily performance and rendering, since it works in a separate process.

Link to Apple documentation and recommendations for using WKWebView https://developer.apple.com/reference/webkit/wkwebview/

Attention!

Starting with iOS 8.0 and OS X 10.10, use WKWebView to add web content to your application. Do not use UIWebView or WebView.

However, it’s very simple to set up your own javascript bridge:

 import WebKit class ViewController: UIViewController, WKScriptMessageHandler { var webView: WKWebView? override func loadView() { super.loadView() webView = WKWebView(frame: self.view.frame) webView?.configuration.userContentController.add(self, name: "scriptHandler") self.view.addSubview(webView!) } public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { print("Message received: \(message.name) with body: \(message.body)") } // rest of code } 

Then in your javascript code call it:

 window.webkit.messageHandlers["scriptHandler"].postMessage("hello"); 

I wrote a library that uses this and adds some fantastic javascript syntax. https://github.com/tmarkovski/BridgeCommander

To use it, just reference the project (or add quick and javascript files to your Xcode project) and call

  webView = WKWebView(frame: self.view.frame) let commander = SwiftBridgeCommander(webView!) commander.add("echo") { command in command.send(args: "You said: \(command.args)") } 

Then you can use javascript callback syntax like this

 var commander = new SwiftBridgeCommander(); commander.call("echo", "Hello", function(args) { // success callback }, function(error) { // error callback }); 
+10
source

Source: https://habr.com/ru/post/1260202/


All Articles