phonegap: open external page and then go back to app
Asked Answered
C

3

7

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

Civic answered 17/10, 2012 at 18:32 Comment(0)
E
3

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();
    }
}; 
Encincture answered 17/10, 2012 at 20:8 Comment(3)
Thank you so much. By the way, since this is just i workaround to another problem, is there a way to go back to a local page from an external page that i cannot edit, but that can receive a callback url as a parameter?Civic
@SimonMacDonald : The url points to a 404 one. Is this still the valid solution?Hoxha
@Simon MacDonald ChildBrowser plugin link is dead.Pastelki
G
1

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 ];
}
    }
Gond answered 5/11, 2012 at 9:12 Comment(2)
Thanks. This seems pretty iphone-related i'll check it out on cordova for android too.Civic
yes it is iPhone related. You shouldn't have any problems with cordova-androidGond
D
1

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.

Dorison answered 6/6, 2013 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.