0
我嘗試打開我的本地html鏈接,但我不知道我的代碼中有什麼問題。我將UIWebViewDelegate添加到類中,並將代理添加到View中。在Safari瀏覽器打開鏈接不uiwebview
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
}
任何人都可以幫我嗎?謝謝!
編輯:打印(request.URL)顯示輸出,所以他一躍而起功能,但Safari瀏覽器沒有打開
我複製並粘貼您的代碼在Xcode中,沒有問題。你面臨什麼問題?日誌中的任何消息? – chengsam
'UIApplication.sharedApplication()。openURL(request.URL!)'不會被執行,因爲你已經把它作爲註釋。或者這是打算? – FelixSFD
爲了測試的原因,我把它放在評論中。如果ja單擊鏈接,則不會發生任何事情,openURL將不會執行 – dtothepipo