Html 从UIWebView打开Safari中的特定链接
我有一个Html 从UIWebView打开Safari中的特定链接,html,ios,swift,safari,uiwebview,Html,Ios,Swift,Safari,Uiwebview,我有一个UIWebView,它加载一个本地index.html文件 但是,我想在Safari中打开此html文件中的外部链接,而不是在UIWebView中打开 从safari中的ui按钮打开链接非常简单: UIApplication.sharedApplication().openURL(NSURL(string: "http://www.stackoverflow.com")) 从外部链接打开Instagram应用程序也很有魅力 <a href="instagram://media?i
UIWebView
,它加载一个本地index.html文件
但是,我想在Safari中打开此html文件中的外部链接,而不是在UIWebView
中打开
从safari中的ui按钮打开链接非常简单:
UIApplication.sharedApplication().openURL(NSURL(string: "http://www.stackoverflow.com"))
从外部链接打开Instagram应用程序也很有魅力
<a href="instagram://media?id=434784289393782000_15903882">instagram://media?id=434784289393782000_15903882</a>
这是你可以做到的
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
}
嗯,似乎还是有点不对劲。我已经添加了我的swift代码,包括你的函数。你能帮我看看出了什么问题吗?非常感谢!您是否已指定,UIWebView
delegate?
import UIKit
class 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.
}
}
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
}