为什么UIWebView无法在Safari中打开我的链接?

时间:2016-07-22 17:07:37

标签: ios swift safari uiwebview

我写了这段代码:

    if navigationType == UIWebViewNavigationType.LinkClicked {
        UIApplication.sharedApplication().openURL(request.URL!)
        return false
    }

它适用于所有普通网址。但是当我用下一个代码点击图像/ gif时:

<a href="https://cdn-images-1.medium.com/max/800/1*IR7oti7mEsYunzp_HhcNig.gif" 
target="_blank" class="c" 
style="background-image: url('//cdn.iframe.ly/pj?url=https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1*IR7oti7mEsYunzp_HhcNig.gif&amp;width=1280&amp;
key=9b0e65e32a5412cf736c430972d914c33f46250b&amp;cache=temp');">
1*IR7oti7mEsYunzp_HhcNig.gif </a>

它在我的应用程序中打开了这个gif / URL,在UIWebView中。

我怎么强迫它也会在Safari中打开那个gif / URL?

1 个答案:

答案 0 :(得分:0)

必须在代码中的其他位置破坏某些内容。 工作正常如下:

import UIKit

class ViewController: UIViewController, UIWebViewDelegate {

@IBOutlet var webView : UIWebView?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.webView?.loadHTMLString("<a href='https://cdn-images-1.medium.com/max/800/1*IR7oti7mEsYunzp_HhcNig.gif' target='_blank' class='c' style='background-image: url('//cdn.iframe.ly/pj?url=https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1*IR7oti7mEsYunzp_HhcNig.gif&amp;width=1280&amp; key=9b0e65e32a5412cf736c430972d914c33f46250b&amp;cache=temp');'> 1*IR7oti7mEsYunzp_HhcNig.gif </a>", baseURL: NSURL(string: "testurl.com"))
}

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if navigationType == UIWebViewNavigationType.LinkClicked {
            UIApplication.sharedApplication().openURL(request.URL!)
            return false

        }

    return true
}
}

Screenshot with the result