URL Redirection within the same WebView (Android)

The default behaviour of a PhoneGap when a user taps on a web link is to open the default browser app and run the web link. However, it might make better sense for the web link to open in the same PhoneGap webview instance if the web link was intended to load a remote HTML5 app that should run in the PhoneGap enviroment on the device.

In com.phonegap.DroidGap class, add in the following lines of code to process URLs starting with “androidurl://”. The problem was that “http://” URLs somehow do not trigger the “shouldOverrideUrlLoading()” event, so the workaround was to use “androidurl://” within the HTML5 app to trigger the event on the native app and then revert the URL to “http://” and process it accordingly.

Codes:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

...

// All else
else {
// IMPT! Fix for shouldOverrideUrlLoading() to be triggered from window.location javascript
if (url.startsWith("androidurl://")) {
url = url.replaceAll("androidurl://", "http://");
}

...
}

Advertisements
Tagged , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: