简体   繁体   English

从UIWebView中的链接打开iOS6 Apple Maps应用程序

[英]Open iOS6 Apple Maps app from a link in a UIWebView

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. 它在标准iPhone Safari浏览器中单击时打开原生iOS6 Apple Maps应用程序。 When I display the same webpage in a UIWebView inside my app and then click the link, my delegate method 当我在我的应用程序内的UIWebView中显示相同的网页,然后单击链接,我的委托方法

- (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 如果委托方法返回YES以指示UIWebView应该继续并打开链接,则会立即触发对委托的新调用,这次参数是

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. 并且可以预见地返回YES ,这会导致我的UIWebView中的maps.google.com网页打开。 But I actually did want the iOS6 Apple Maps app to launch. 但实际上我确实想要推出iOS6 Apple Maps应用程序。 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? 我可以尝试一个CLGeocoder从URL创建一个MKMapItem ,沿着如何在iOS 6中启动具有特定地址的iOS Maps App? , but CLGeocoder seems to want plain text and not a url with all its conventional annotation, quite a detailed parsing job. ,但CLGeocoder似乎想要纯文本,而不是一个带有所有常规注释的url,非常详细的解析工作。 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. 如果我以不同于Mobile safari的方式解析URL,那么我可能会看到不同的地图结果,具体取决于我是否通过我的UIWebView或Safari从相同的初始链接到达地图应用程序。

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? 有没有办法让UIWebView从链接打开iOS6 Apple Maps应用程序,而无需解析然后对URL进行地理编码?

As an aside, after my delegate method is called with the maps.google.com url, it's called twice with these parameters: 另外,在使用maps.google.com url调用我的委托方法后,使用以下参数调用它两次:

url=about:blank 
navigationType=UIWebViewNavigationTypeOther

and that eventually has no effect on the display. 并且最终对显示器没有影响。 Any idea what that's about? 知道这是什么意思吗?

Yes, there is a way to open a maps.apple.com link into the Maps app from a UIWebview. 是的,有一种方法可以从UIWebview打开maps.apple.com链接到Maps应用程序。 Don't have the UIWebview open the link directly, but pass it off to the OS to sort out. 没有UIWebview直接打开链接,但将其传递给操作系统进行整理。 In webView: shouldStartLoadWithRequest: navigationType: , I look for the maps link and treat it specially: webView: shouldStartLoadWithRequest: navigationType: ,我查找地图链接并特别处理它:

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. 这将打开iOS6中的Apple Maps应用程序。 In iOS5, it will open the google maps webpage in Safari. 在iOS5中,它将在Safari中打开谷歌地图网页。 I'll look for iOS5 systems and treat the URL differently there, too. 我会寻找iOS5系统,并在那里对URL进行不同的处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM