Calling window.location.href in PhoneGap triggers web browser
Asked Answered
K

2

9

Hello I'm trying to develop an application for iPad using PhoneGap. I would like to dinamically load inside the index.html page the main page of an external website. Unfortunately using

window.location.href = "http://mywebsite.com/cgi-bin/index.py"

triggers the opening of a Safari window instead of using the PhoneGap container.

Any suggestions?

Thanks a lot

Claus

Kostival answered 5/5, 2011 at 12:51 Comment(2)
This other question has the right answer: #5911755Solfa
The answer can be found over here: #5911755Solfa
G
3

Find the AppDelegate.m file in the 'Classes' part of the project, and find webView:shouldStartLoadWithRequest:navigationType Make the function look like this and try again!

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
        [[UIApplication sharedApplication] openURL:url];    
        return NO;
    }
    else {
       return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }
}
Gunnel answered 17/10, 2011 at 23:50 Comment(0)
S
5

There's a simpler option: modify config.xml

Open all links in WebView

stay-in-webview with values true or false

  • example: <preference name="stay-in-webview" value="true" />

  • if set to true, all links (even with target set to blank) will open in the app's webview

  • only use this preference if you want pages from your server to take over your entire app

  • default is false

Source: https://build.phonegap.com/docs/config-xml

Salman answered 8/9, 2012 at 20:54 Comment(0)
G
3

Find the AppDelegate.m file in the 'Classes' part of the project, and find webView:shouldStartLoadWithRequest:navigationType Make the function look like this and try again!

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
        [[UIApplication sharedApplication] openURL:url];    
        return NO;
    }
    else {
       return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }
}
Gunnel answered 17/10, 2011 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.