Question

I'm building an app that uses the InAppBrowser quite a lot. At some point, the user is able to click an external link from within this window. I tried diffrent methods, but none seems to get a good working result.

The best solution so far is listening to the loadstart event (As described here):

app.browser.addEventListener('loadstart', function (inAppBrowser) {
    if(inAppBrowser.url.match(/domain\.com/) === null) {
        var url = inAppBrowser.url;
        window.open(url, "_system");
    }
}

This opens the link in a new window, but also in the original InAppBrowser. Is it possible to cancel this event? Or is there a other approach i can try?

I already tried the following approaches:

This is for iOS specific.

EDIT: I ended up by adding this code in platforms/ios/APPNAME/Plugins/org.apache.cordova.inappbrowser/CDVInAppBrowser.m:

NSString *domainStr = [NSString stringWithFormat:@"domain.com"];
NSString *urlStr = [NSString stringWithFormat:@"%@", request.URL];
NSRange result = [urlStr rangeOfString:domainStr];

if(result.location == NSNotFound) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];

    return NO;
}

above this code:

return [self.navigationDelegate webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
Was it helpful?

Solution

You have some options:

  • hide the external links injecting css to inappbrowser, if they don't appear, then they can't be clicked

add a loadstop listener and then hide the links

app.browser.addEventListener('loadstop', hideLinks);



function hideLinks(){
    app.browser.insertCSS({
                      code: "a { display: none; }"
                      }, function() {
                      console.log("Styles Altered");
                      });
}
  • modify/subclass inappbrowser, changing the shouldStartLoadWithRequest method

change the return [self.navigationDelegate webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType]; to return NO if the URL isn't yours

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top