Phonegap: open an external page and then return to the application

ok, looking through the questions for which I found the right way to load an external page in the form of a phone call (i.e. without losing a session or opening the device’s browser), as described here: How can I load a web page inside the phonegap website? and here: Phonegap for iPhone: the problem of loading an external URL

Next step: after I opened the extarnal page (it belongs to me and I can change it), how can I return to my local application? Say I have a link on an external page, and I want the user to be redirected back to the local html page (mypage.html) inside the phonegap application when clicked.

What url should the link href attribute have? I tried installing it in the file: ///android_asset/www/mypage.html, but did not work

+7
source share
3 answers

You want to use the ChildBrowser plugin to open an external web page. Then you want to set the ChildBrowser.onLocationChange property to your own function. Then, when a person moves from a remote page, you will be notified of a change in location, then to close ChildBrowser and go to a new local page. You won’t even have to touch the remote html page.

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

cb.onLocationChange = function(loc){ console.log("location = " + loc); if (loc != "http://example.com") { cb.close(); } }; 
+3
source

What you need is a caster 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 ]; } } 
+1
source

This is used by PhoneGap / Cordova 2.7. Inside the external application, add a link pointing 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.

+1
source

All Articles