i try to open links from my local html but i don't know what is wrong in my code. I added UIWebViewDelegate to the class and the delegate to the 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
}
can anybody help me? Thanks!
edit: print(request.URL) is shown in the output, so he jumps into the function, but safari don't open
Make sure your UIWebViews
delegate property in IB
is connected to your ViewController
. Otherwise the method you implemented will never be called. Go to the outlets pane on IB
for the WebView
and connect the delegate outlet with your ViewController
that implements the UIWebViewDelegate
protocol.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.