Reputation: 2203
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:
history.back(-1)
via the executeScript method.window.open(url, '_system');
from within the InAppBrowser.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];
Upvotes: 1
Views: 2981
Reputation: 53351
You have some options:
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");
});
}
shouldStartLoadWithRequest
methodchange the return [self.navigationDelegate webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
to return NO
if the URL
isn't yours
Upvotes: 1