2

I apologize if this has been asked but I was unable to find any answer or solution. I am looking to call a JavaScript function from a "UIWebView" and listen for it in swift. Any example I have found uses "WKWebView". There has to be an easy way to listen to a JavaScript function like the below:

// HTML / JS
<div id ="myDiv" onclick="listenInSwift()">myItem</div>

Is this possible with a UIWebView? Thanks all!

user3612986
  • 315
  • 2
  • 7
  • 18

2 Answers2

13

Implement listenInSwift like this:

function listenInSwift() {
    window.location = 'yoururlscheme://somehost?greeting=hello'
} 

Then listen for this URL with this code in your UIWebViewDelegate class:

func webView(_ webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType navigationType: UIWebViewNavigationType) -> Bool {
    if request.URL.scheme == 'yoururlscheme' {
        print('Hello JavaScript...')
    }
}

Don't forget to register your URL Scheme (in this case 'yoururlscheme') in Xcode.

To load a local file in the web view, try this:

let baseURL = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath);
let relativePath = "www/\(file)"
let fileURL = NSURL(string: relativePath, relativeToURL: baseURL);
let URLRequest = NSURLRequest(URL: fileURL!);
webView.loadRequest(URLRequest)
paulvs
  • 11,963
  • 3
  • 41
  • 66
  • Thanks paulvs, will this work if the url scheme is local? How do I get the local path to the html file embedded in xocde? – user3612986 Mar 22 '16 at 17:51
  • The URL Scheme must be local (for how to create one, [look here](https://dev.twitter.com/cards/mobile/url-schemes)). – paulvs Mar 23 '16 at 01:46
  • What is the equivalent of `UIWebView` in `OS X` `Swift`? `UIKit` is not available for import when coding for `OS X`. And `WebKit` doesn't perform the function you described. – dbconfession Jun 22 '16 at 20:50
  • I haven't used it, but [WebView](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/WebKit/Classes/WebView_Class/) does seem to be the OS X version of `UIWebView`. – paulvs Jun 23 '16 at 01:45
  • it's not working in swift3 anymore.. any suggestions ? – Amir Foghel Dec 19 '16 at 18:57
  • just note - newer version is "func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {" + do not forget to set "webView.delegate = self", otherwise it's not firing – Tunerx Jan 24 '17 at 20:34
  • Enormous thank to you. Simply and exactly what I needed – SwiftStudier Feb 14 '17 at 20:19
  • what if i want to return a value from the function listenInSwift()? – Raghuram Apr 13 '17 at 10:20
  • @Raghuram I explained some approaches for the `UIWebView` here: http://stackoverflow.com/questions/26851630/javascript-synchronous-native-communication-to-wkwebview – paulvs Apr 15 '17 at 16:09
  • instead of using `window.location`, can i simple use ajax? – Maria Oct 16 '18 at 12:55
  • how do u ge t the param In swift – Alberto Acuña Dec 01 '18 at 21:16
0

this worked fine for me:

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    if request.url?.scheme == "com.example.app" {
        print("I’m some JavaScript hoho")
    }

    return true
}

Note: you have to add "com.example.app" or whatever in your xcode project settings. Go to info, scroll down and find URL Scheme. Add your desired scheme there. Leave the other stuff the way it is. Then it should work just fine.

Adrian
  • 13
  • 4