答案 0 :(得分:0)
您应该实现委托WKNavigationDelegate并定义函数decisionPolicyForNavigationAction,例如:
class ViewController: UIViewController, WKNavigationDelegate {
...
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
webView = WKWebView(frame: CGRect(x: 0, y: 50, width: 100, height: 100))
webView.navigationDelegate = self
webView.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.example.com")!))
view.addSubview(webView)
}
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
let path = navigationAction.request.URL?.absoluteString
if let path = path {
if path.rangeOfString("mp4") != nil {
// This is video link, don't let iOS open video
decisionHandler(.Cancel)
print("Process video \(path)")
return
}
}
// Any other link should be handled by WKWebView
decisionHandler(.Allow)
}
检测视频链接是否包含路径中的mp4或您要检测的其他扩展名或路径,然后自行处理。