解析网址,以便仅映射链接从UIWebView中在外部应用程序中打开? [英] Parse url so that only maps links open in external app from within UIWebView?

查看:54
本文介绍了解析网址,以便仅映射链接从UIWebView中在外部应用程序中打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中的UIWebView中包含Web内容.我的目标是在应用程序内正常打开所有链接,但以"http://maps"开头的任何链接都可以在safari中打开,以便可以依次在外部iPhone地图应用程序中打开它们.如果您有解决此问题的解决方案,请立即停止阅读,下面我将提出解决方案.当前,所有链接都在应用程序内打开,因此 http://maps 链接在应用程序内打开至m.google.com.我正在考虑的解决方案涉及此代码,该代码使用openURL打开safari中的所有链接:

I have web content inside a UIWebView in my app. My goal is to have all links open normally inside the app, but any links starting with "http://maps", get opened in safari, so they can in turn be opened in the external iphone maps app. If you have a solution for this problem stop reading now, below I'm going to propose my solution. Currently all links are opened inside the app, so http://maps links open to m.google.com inside the app. The solution I'm thinking of involves this code which uses openURL to open all links in safari:

(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        \[\[UIApplication sharedApplication] openURL:request.URL];
        return false;
    }
    return true;
}

显然,此代码的问题是所有链接都在Safari中打开,而我只想要地图链接.您能否建议一种方法来解析链接,并仅通过该函数传递以 http://maps 开头的链接?另外一个更简单的问题是,如何委派UIWebView以便我可以运行此代码,并且viewcontroller.m也是放置此代码的正确位置吗?

Obviously the problem with this code is that all links are opened in safari, and I only want map links. Can you suggest a way to parse through links and only pass ones that start with http://maps through the function? Also a more simple question, how do I delegate UIWebView so I can run this code, and also is the viewcontroller.m the right place to put this code?

如果你们可以建议一个完整的功能,包括上面的openURL部分和链接解析,以确保只有maps链接通过该功能真棒.同样,如果您有其他解决方案或解决方法,我很想听听.非常感谢您的帮助,stackoverflow真是救命稻草,我的第一个项目快要完成了!

If you guys could suggest an entire function, including the openURL part above and the link parsing to make sure only maps links get passed through the function that would be awesome. Again, if you have another solution or workaround I would love to hear it. Thanks so much for your help, stackoverflow has been a lifesaver, I'm almost finished with my first project ever!

推荐答案

试试这个:

-(BOOL)webView:(UIWebView *)inWeb
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)inType {
  // let UIWebView deal with non-click requests
  if (inType != UIWebViewNavigationTypeLinkClicked) {
    return YES;
  }

  // URL starts with "http://maps"?
  if ([[request.URL description] hasPrefix:@"http://maps"]) {
    // open URL in Safari and return NO to prevent UIWebView from load it
    [[UIApplication sharedApplication] openURL:[request URL]];
    return NO;
  }

  // otherwise let UIWebView deal with the request
  return YES;
}

这篇关于解析网址,以便仅映射链接从UIWebView中在外部应用程序中打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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