Amir Foghel
Amir Foghel

Reputation: 975

UIWebView stopped getting JS events in shouldStartLoadWith under Swift3

I'm using UIWebView in my application for some reasons that WKWebView can't fit for me. Explaining that, I just finished with my application conversion to Swift3 (Was 2.2) and my shouldStartLoadWith functions not getting the JS event. If i run my previous build before the conversion - everything working perfect.

My code look like this:

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {

        let url:URL = request.url!
        if (url.scheme == "something"){

            if (url.host != nil){
                var properString = url.absoluteString.removingPercentEncoding!
                properString = properString.replacingOccurrences(of: "something://", with: "")
                performJSFunction(message: properString)
            }

            return false;
        }

        return true
    }

note: nothing changes in the server side/html page.

Upvotes: 3

Views: 412

Answers (2)

Machado
Machado

Reputation: 14499

For those who are trying to do the same job but using local html files this doesn't have nothing to do with https:// or http://.

The solution was changing the <button> to the <a> tag.

<a onclick="interface.callFromJS()" href="inapp://capture" ontouchstart="">Your Button Message</a>

And then you'll be able to catch these events there:

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {

        if (request.url?.scheme == "inapp")
        {
            doYourAction()      
        }

        return true

    }

Hope it helps someone.

Upvotes: 0

Amir Foghel
Amir Foghel

Reputation: 975

After many tries i have found out that the UIWebView will get events with https:// or http://in the beginning.

so if you used to get it like this:

appname://something

now you should get it like this:

https://appname//something

(pay attention that the dots after the "appname" will be remove automatically - even if you will send it via src attribute.

i know it's not what you wanted to be - but it works really good.

Upvotes: 2

Related Questions