If I display a link to a map in a webpage
<a href="http://maps.apple.com/?q=Pricketts+Hill+Southampton+Hampshire+SO32+2JW&ll=50.913127,-1.191398">Location</a>
it opens the native iOS6 Apple Maps app when clicked in the standard iPhone Safari browser. When I display the same webpage in a UIWebView inside my app and then click the link, my delegate method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
is called, and the parameters are
url=http://maps.apple.com/?q=Pricketts+Hill+Southampton+Hampshire+SO32+2JW&ll=50.913127,-1.191398
navigationType=UIWebViewNavigationTypeLinkClicked
If the delegate method returns YES
to indicate that the UIWebView should go ahead and open the link, a new call to the delegate is immediately triggered, and this time the parameters are
url=http://maps.google.com/?q=Pricketts+Hill+Southampton+Hampshire+SO32+2JW&ll=50.913127,-1.191398
navigationType=UIWebViewNavigationTypeLinkClicked
and returning YES
to this, predictably, results in the maps.google.com webpage opening in my UIWebView. But I actually did want the iOS6 Apple Maps app to launch. I could try a CLGeocoder
to create an MKMapItem
from the URL, along the lines of How to launch iOS Maps App with specific address in iOS 6?, but CLGeocoder
seems to want plain text and not a url with all its conventional annotation, quite a detailed parsing job. And if I parse the URL differently from the way Mobile safari does it, then I may see different map results depending on whether I reached the Maps app via my UIWebView or Safari from the same initial link.
Is there some way to get a UIWebView to open the iOS6 Apple Maps app from a link without having to parse and then geocode the URL?
As an aside, after my delegate method is called with the maps.google.com url, it's called twice with these parameters:
url=about:blank
navigationType=UIWebViewNavigationTypeOther
and that eventually has no effect on the display. Any idea what that's about?
Parameters. There are multiple parameters you can add to the Apple Maps URLs and the https://maps.apple.com/ URL.
A view that embeds web content in your app.
Use Apple Maps on Android Smartphones and TabletsVisit DuckDuckGo on any mobile browser and search for a location. Next, click on Maps at the top to access Apple Maps. Click on Open Map to see the location on Apple Maps or select Directions on the next page to see a route mapped out for you.
Yes, there is a way to open a maps.apple.com link into the Maps app from a UIWebview. Don't have the UIWebview open the link directly, but pass it off to the OS to sort out. In webView: shouldStartLoadWithRequest: navigationType:
, I look for the maps link and treat it specially:
if (([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"]) && [url.host isEqualToString:@"maps.apple.com"]) { //it's an apple maps app request
NSLog(@"Attempting Apple Maps app open");
[[UIApplication sharedApplication]openURL:url];
return NO;
}
And that opens the Apple Maps app in iOS6. In iOS5, it will open the google maps webpage in Safari. I'll look for iOS5 systems and treat the URL differently there, too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With