本文介绍了从UIWebView打开Safari中的特定链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个加载本地index.html文件的 UIWebView 。然而,我有一个外部链接在这个html文件中,我想在Safari中打开而不是在 UIWebView 中内部打开。 打开Safari中的链接来自 UIButton 很简单: UIApplication。 openURL(NSURL(string:http://www.stackoverflow.com)) < a href =instagram:// p>从外部链接中打开Instagram应用程序也很有用。 ?介质ID = 434784289393782000_15903882\" >的Instagram://介质ID = 434784289393782000_15903882< / A>? 所以我的第一个尝试是这样做的: < a href =safari://stackoverflow.com>在Safari中打开< / a> 然而,这似乎并不奏效,然后我读了一些关于使用 webView:shouldStartLoadWithRequest:navigationType: 但是每个设法在Safari中打开外部链接的人都是用Obj-C编写的,太熟悉了,因为我正在写Swift。 使用Swift代码进行更新: 导入UIKit 类附件视图控制器:UIViewController,UIWebViewDelegate { @IBOutlet弱var webView:UIWebView! 重写func viewDidLoad(){ super.viewDidLoad() $ b $如果让url = NSBundle.mainBundle()。URLForResource(accessories ,withExtension:html){ webView.loadRequest(NSURLRequest(URL:url))} $ b} 覆盖func preferredStatusBarStyle() - > UIStatusBarStyle {返回UIStatusBarStyle.LightContent; } func webView(webView:UIWebView,shouldStartLoadWithRequest request:NSURLRequest,navigationType:UIWebViewNavigationType) - > Bool { if let url = request.URL where navigationType == UIWebViewNavigationType.LinkClicked { UIApplication.sharedApplication()。openURL(url) return false } 返回true } 覆盖func didReceiveMemoryWarning(){ super.didReceiveMemoryWarning() //处理任何可以重新创建的资源。 } } 解决方案 func webView(webView:UIWebView,shouldStartLoadWithRequest请求:NSURLRequest ,navigationType:UIWebViewNavigationType) - > Bool { if let url = request.URL where navigationType == UIWebViewNavigationType.LinkClicked { UIApplication.sharedApplication()。openURL(url) return false } 返回true } I have a UIWebView which loads a local index.html file.However I have an external link in this html file that I'd like to open in Safari instead of internally in the UIWebView.Opening a link in safari from say a UIButton was simple enough:UIApplication.sharedApplication().openURL(NSURL(string: "http://www.stackoverflow.com"))Opening the Instagram app from a external link also works like a charm.<a href="instagram://media?id=434784289393782000_15903882">instagram://media?id=434784289393782000_15903882</a>So my first though was to do something like this:<a href="safari://stackoverflow.com">Open in Safari</a>However that doesn't seem to work, then I read something about using webView:shouldStartLoadWithRequest:navigationType:But everyone who's managed to open an external link in Safari is writing in Obj-C which I'm not too familiar with as I'm writing in Swift.Update with Swift Code:import UIKitclass AccessoriesViewController: UIViewController, UIWebViewDelegate { @IBOutlet weak var webView:UIWebView! override func viewDidLoad() { super.viewDidLoad() if let url = NSBundle.mainBundle().URLForResource("accessories", withExtension: "html") { webView.loadRequest(NSURLRequest(URL: url)) } } override func preferredStatusBarStyle() -> UIStatusBarStyle { return UIStatusBarStyle.LightContent; } func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { if let url = request.URL where navigationType == UIWebViewNavigationType.LinkClicked { UIApplication.sharedApplication().openURL(url) return false } return true } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }} 解决方案 Here is how you can do it!func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { if let url = request.URL where navigationType == UIWebViewNavigationType.LinkClicked { UIApplication.sharedApplication().openURL(url) return false } return true } 这篇关于从UIWebView打开Safari中的特定链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-12 09:34