0

I'm building an iOS (swift) app that contains a UIWebView. The webpage presented in the webview calls a javascript method with some info that I'm catching with an injected javascript object.

The following is the implementation of said method:

window.location.href = "customScheme://someInfo";

And I catch that with the delegate as shown below:

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

    if (request.URL?.scheme == "customScheme") {
        print(request.URL?.host)
        return false
    }

    return true
}

func webViewDidStartLoad(webView: UIWebView) {
    if let
        path = NSBundle.mainBundle().pathForResource("JavaScript", ofType: "js"),
        script = try? String(contentsOfFile: path, encoding: NSUTF8StringEncoding) {
            self.webview?.stringByEvaluatingJavaScriptFromString(script)
    }
}

The method is catched as expected and retrieving the information as wanted BUT the problem is that when the method is catched, the webview just stops loading and remains blank.

I tried the following instead:

alert('test'); // COMMENTED window.location.href = "customScheme://someInfo";

And the alert was shown and the page loaded perfectly fine, any idea on how to solve this? I'm not very good at javaScript, should I use another method instead of window.location.ref?

I tried with this methods as well:

window.open('customScheme://someInfo')
window.location.replace('customScheme://someInfo')
window.location.assign('customScheme://someInfo')
window.location.href = 'customScheme://someInfo'
document.location.href = 'customScheme://someInfo'
self.location = 'customScheme://someInfo';
top.location = 'customScheme://someInfo';

But either it doesn't trigger the 'shouldStartLoadWithRequest' or I get the blank page again :/

mtet88
  • 524
  • 6
  • 21

0 Answers0