Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phonegap: open external page and then go back to app

ok, browsing the questions I've found the right way to load an external page into the phonegap view (i.e. without loosing the session or opening the device browser) as explained here: How can I load a webpage inside the phonegap webview? and here: PhoneGap for iPhone: problem loading external URL

Next step is: after i've opened an extarnal page (it's owned by me and I can modify it) how can i go back to my local application? Let's say I have a link in the external page and I want the user to be redirected back to a local html page (mypage.html) inside the phonegap application on click.

What url should the link's href attribute have? I've tried setting it to "file:///android_asset/www/mypage.html" but didn't work

like image 479
Marco Gagliardi Avatar asked Oct 17 '12 18:10

Marco Gagliardi


3 Answers

You want to use the ChildBrowser plugin to open the external web page. Then you want to set the ChildBrowser.onLocationChange property to your own function. Then when the person navigates away from the remote page you will be notified of the location change so you can then close the ChildBrowser and navigate to a new local page. You won't even need to touch the remote html page.

So to close the browser when the user navigates away from the remote page:

cb.onLocationChange = function(loc){
    console.log("location = " + loc);
    if (loc != "http://example.com") {
        cb.close();
    }
}; 
like image 184
Simon MacDonald Avatar answered Nov 19 '22 16:11

Simon MacDonald


What you need is this charmer in your MainViewController.m It works for me in cordova 1.7.0 cordova 1.9.0 and cordova 2.1.0

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request         navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];

// Intercept the external http requests and forward to Safari.app
// Otherwise forward to the PhoneGap WebView
if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
    }
like image 1
iOSAndroidWindowsMobileAppsDev Avatar answered Nov 19 '22 16:11

iOSAndroidWindowsMobileAppsDev


This is using PhoneGap/Cordova 2.7. Inside your external app, add a link that points to "app://index".

Inside onCreate add:

this.appView.setWebViewClient(new CordovaWebViewClient(this, this.appView) {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.equalsIgnoreCase("app://index")) {
            Log.d("DEBUG", url);

            loadUrl(Config.getStartUrl());

            return true;
        } else {
            return super.shouldOverrideUrlLoading(view, url);
        }
    }
});

This will intercept the call and redirect the user to the configured start url.

like image 1
Dustin Avatar answered Nov 19 '22 16:11

Dustin