我的应用内部有一个网络视图。在允许/取消请求之前,我正在查看每个请求并查看它们是否是YouTube视频:
extension WebBrowserViewController: WKNavigationDelegate {
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
if (urlVideoType(webView.URL!) != VideoUrl.Unknown && urlVideoType(webView.URL!) != VideoUrl.Youtube) {
...
} else if urlVideoType(webView.URL!) == VideoUrl.Youtube {
// the success and failure part don't work
presentYoutubeVideoIfAvailable(webView.URL!, success: decisionHandler(.Cancel), failure: decisionHandler(.Allow)) <-------------------
} else {
decisionHandler(.Allow)
}
}
}
我的presentYoutubeVideoIfAvailable
方法使用此youtube网址解析器窗格:https://github.com/movielala/YoutubeSourceParserKit
但是,这会产生异步调用,我不知道如何在我的方法中调用成功和失败方法:
func presentYoutubeVideoIfAvailable(url: NSURL, success: (), failure: ()) {
Youtube.h264videosWithYoutubeURL(url) { [unowned self] (videoInfo, _) -> Void in
switch videoInfo?["url"] as? String {
case .Some(let videoUrlString):
VideoStore.addVideo(url.absoluteString, title: videoInfo?["title"] as? String ?? "Unknown")
success
self.presentVideo(NSURL(string: videoUrlString)!)
case .None:
print("herereee") // gets printed, so I know it's here
failure // doesn't do anything. it should be allowing the request but it isn't <-----------------------------------
self.showError(.YoutubeParsingFail)
}
}
}
我想要它,以便如果youtube pod返回一个url,它应该取消web视图请求,如果它失败,我希望它继续请求。我该怎么做?
答案 0 :(得分:1)
这里没有发生任何事情的原因是你没有调用闭包。应该像swift中的任何其他函数一样调用闭包,如下所示:
success()
failure()