在safari中打开链接而不是uiwebview

时间:2016-07-22 15:58:37

标签: ios swift uiwebview

我尝试从我的本地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没有打开

1 个答案:

答案 0 :(得分:0)

确保UIWebViews中的IB委托属性与您的ViewController相关联。否则,将永远不会调用您实现的方法。转到IBWebView的出口窗格,并将代理出口与实施ViewController协议的UIWebViewDelegate相关联。