Open iOS6 Apple Maps app from a link in a UIWebVie

2019-05-01 03:22发布

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?

1条回答
该账号已被封号
2楼-- · 2019-05-01 04:00

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.

查看更多
登录 后发表回答