3

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Amir Foghel
  • 853
  • 1
  • 12
  • 28

2 Answers2

2

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.

Amir Foghel
  • 853
  • 1
  • 12
  • 28
  • And how exactly I'm supposed to get an https/http from a local HTML file? Any ideas to help? – Machado Jan 06 '17 at 13:17
  • if i understand your question i just made it like this: if i get a url started with the src attribute i selected (as described in my answer) i didn't let it continue with the process and if not - i acting like a regular url – Amir Foghel Jan 08 '17 at 14:42
0

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.

Machado
  • 14,105
  • 13
  • 56
  • 97