从UIWebView中的链接打开iOS6 Apple Maps应用程序 [英] Open iOS6 Apple Maps app from a link in a UIWebView

查看:125
本文介绍了从UIWebView中的链接打开iOS6 Apple Maps应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在网页中显示指向地图的链接

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>

在标准iPhone Safari浏览器中点击后,它会打开原生iOS6 Apple Maps应用程序。当我在我的应用程序内的UIWebView中显示相同的网页,然后单击链接时,我的委托方法

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 {

是调用,参数是

url=http://maps.apple.com/?q=Pricketts+Hill+Southampton+Hampshire+SO32+2JW&ll=50.913127,-1.191398
navigationType=UIWebViewNavigationTypeLinkClicked

如果委托方法返回 YES 以指示UIWebView应该继续并打开链接,立即触发对委托的新调用,这次参数为

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

并返回<可以预见,code> YES 会导致我的UIWebView中的maps.google.com网页打开。但实际上我确实想要推出iOS6 Apple Maps应用程序。我可以尝试 CLGeocoder 从URL创建一个 MKMapItem ,沿着如何在iOS中启动具有特定地址的iOS Maps App 6?,但是 CLGeocoder 似乎想要纯文本而不是带有所有常规注释的url,这是一个非常详细的解析工作。如果我以不同于Mobile safari的方式解析URL,那么我可能会看到不同的地图结果,具体取决于我是否通过我的UIWebView或Safari从相同的初始链接到达地图应用程序。

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.

有没有办法让UIWebView从链接打开iOS6 Apple Maps应用程序而无需解析然后对URL进行地理编码?

顺便说一句,在使用maps.google.com网址调用我的委托方法后,使用这些参数调用它两次:

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?

推荐答案

是的,有一种方法可以打开maps.apple.com链接到地图应用程序来自UIWebview。没有UIWebview直接打开链接,但将其传递给操作系统进行整理。在 webView:shouldStartLoadWithRequest:navigationType:中,我查找地图链接并特别处理:

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;
}

这将打开iOS6中的Apple Maps应用程序。在iOS5中,它将在Safari中打开谷歌地图网页。我会寻找iOS5系统并在那里对URL进行不同的处理。

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.

这篇关于从UIWebView中的链接打开iOS6 Apple Maps应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆