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)") }
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) {