我需要使用iframe嵌入Youtube视频。根据测试项目,我尝试使用youtube_ios_player_helper功能
-(BOOL)loadWithVideoId:(nonnull NSString *)videoId playerVars: (nullable NSDictionary *)playerVars
let vars = ["origin": "http://www.youtube.com"]
playerView.load(withVideoId: "gqbB8-sBuvg", playerVars: vars)
视频正在运行。如果我不使用其他参数,我会看到:"This video is unavailable"
。但对于iframe,我只看到"This video is unavailable"
。我正在使用以下html:
videoWebView.loadHTMLString("<iframe width=\"\(videoWebView.frame.width)\" height=\"\(videoWebView.frame.height)\" src=\"https://www.youtube.com/embed/gqbB8-sBuvg?enablejsapi=1&origin=\"http://www.youtube.com\"\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen> </iframe>", baseURL: nil)
如何修复iframe嵌入?
要解决iframe嵌入问题,您无需使用YouTube帮助程序库。问题出在baseURL上。不需要接收nil,而是需要有资源的链接或路径。
例如,
videoWebView.loadHTMLString("<iframe>...</iframe>", baseURL: URL(string: "http://www.youtube.com")!)