Opening Google Map from UIWebView

April 10, 2009 § 2 Comments

iPhone OS 1.0+ use this URL method to open maps in the map app

http://maps.google.com/q=<query string>

iPhone OS 2.0+ use another URL method to open maps in the map app

maps:q=<query string>

UIWebView won’t open maps app by default.

You can consider using stringByAppendingString to add q=<query string>to maps:.

Below is not related to opening Google map app from UIWebView. However, it is nice to be able to open content links within UIWebView in Safari.

You can use openURL: method to open any links in safari within UIWebView. By default, links are opened within UIWebView. Target=’_blank’ option on url won’t work on UIWebView. It won’t open another new window using Safari app.

(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:request.URL];
return false;
}
return true;
}

Another approach

(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; {

NSURL *requestURL =[ [ request URL ] retain ];
// Check to see what protocol/scheme the requested URL is.
if ( ( [ [ requestURL scheme ] isEqualToString: @”http” ]
|| [ [ requestURL scheme ] isEqualToString: @”https” ] )
&& ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {
return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
}
// Auto release
[ requestURL release ];
// If request url is something other than http or https it will open in UIWebView.
return YES;
}

Advertisements

§ 2 Responses to Opening Google Map from UIWebView

  • Johannes says:

    Hi,
    could you elaborate on “UIWebView won’t open maps app by default.”?

    If I have a link like “http://maps.google.com/q=” or “maps:q=” within the webview, wont the google maps app open if I click it?

    Best,
    Johannes

  • johannes says:

    ok, got it.
    Inserting e.g. Cupertino will make a lnk open the map app within a webview.

    Best,
    Johannes

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

What’s this?

You are currently reading Opening Google Map from UIWebView at Web Builders.

meta

%d bloggers like this: