我尝试从我的本地HTML打开链接,但我不知道我的代码有什么问题。我将UIWebViewDelegate添加到类中,并将代理添加到View中。
class TableWebViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var infoWebView: UIWebView!
var htmlName:String!
override func viewDidLoad() {
super.viewDidLoad()
self.infoWebView.delegate = self
self.loadHtmltoWebview(htmlName)
self.infoWebView.backgroundColor = UIColor.whiteColor()
self.infoWebView.opaque = false
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadHtmltoWebview(name: String) {
let htmlFile = NSBundle.mainBundle().pathForResource(name, ofType: "html")
let url = NSURL(string: htmlFile!)
let request = NSURLRequest(URL: url!)
infoWebView.loadRequest(request)
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.LinkClicked {
//UIApplication.sharedApplication().openURL(request.URL!)
print(request.URL)
return false
}
return true
}
任何人都可以帮助我吗?谢谢!
编辑:print(request.URL)显示在输出中,所以他跳进了这个函数,但是safari没有打开
答案 0 :(得分:0)
确保UIWebViews
中的IB
委托属性与您的ViewController
相关联。否则,将永远不会调用您实现的方法。转到IB
上WebView
的出口窗格,并将代理出口与实施ViewController
协议的UIWebViewDelegate
相关联。