我正在使用WKWebView查看youtube视频。如果我在应用程序中键入我的YouTube视频嵌入链接,它可以正常工作。但是,当我从youbase获取youtube嵌入视频代码时,youtube会显示错误。我已经在我的控制台中检查了我正在接收视频编码,并且它已成功分配给我的变量以获取url链接。我不知道我的Firebase和youtube有什么问题
请帮帮我。
class VideoViewController: UIViewController, WKNavigationDelegate {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
var ref: DatabaseReference!
var videolink = ""
@IBOutlet weak var webView: WKWebView!{
didSet{
webView.navigationDelegate = self
}
}
override func viewDidLoad() {
super.viewDidLoad()
fetchOffers()
getVideo(videoCode: videolink)
}
func fetchOffers(){
let databaseRef = Database.database().reference().child("Helsinki")
databaseRef.queryOrdered(byChild: "bar_name").queryEqual(toValue: self.appDelegate.selectedBarName).observe(.value, with: { snapshot in
if ( snapshot.value is NSNull ) {
print("not found)")
} else {
print(snapshot.value!)
for child in snapshot.children {
let snap = child as! DataSnapshot
let imageSnap = snap.childSnapshot(forPath: "video")
print(imageSnap)
let dataDict = imageSnap.value as! [String : AnyObject]
let videoLink = dataDict["videolink"] as! String
self.videolink.append(videoLink)
print("This is shit", self.videolink)
}
}
})
}
func getVideo(videoCode:String){
let url = URL(string: "https:/www.youtube.com/embed/\(videoCode)")
webView.load(URLRequest(url : url!))
}
答案 0 :(得分:0)
这很明显。您正在调用Async方法来获取视频代码但是没有等待播放。有几种方法可以解决这个问题。
一种简单的方法是在 fetchOffers 方法中移动您的电话 getVideo(videoCode:视频链接),如下所示:
print("This is shit", self.videolink)
self.getVideo(videoCode: videolink)
另一种方法是在这样的 fetchOffers 方法中传递一个处理程序(闭包),它将返回String。
func fetchOffers(success: @escaping ((_ code: String) -> Void), failure: @escaping ((_ error: Error) -> Void)){
// same code just return success
print("This is shit", self.videolink)
success(videolink)
}
现在内部视图控制器调用就像这样:
fetchOffers(success:{[weak self] (code) in
self?.getVideo(videoCode: code)
}) { (error) in
print(error)
}