我已经嵌入了一个youtube iFrame来在UIWebView网页中播放视频。
“在youtube上观看”链接不起作用:
代码是:
<iframe name="player" id="player" src="http://www.youtube.com/embed/{$firstParsedVideo}?HD=1;rel=0;showinfo=0" width="279" height="155"></iframe>
其中$ firstParsedVideo是视频ID。
我在Cocoa Osx WebView中也做了同样的事情,它运行得很好。
由于
答案 0 :(得分:1)
据我所知,Apple不允许您在自己的应用中使用WebView播放Youtube视频,他们也会拒绝您的应用。因为我以前有这方面的经验。
顺便说一下,我写了一个在我的应用中播放Youtube视频的方法:
- (void)embedYouTube:(NSString*)videoId frame:(CGRect)frame {
NSString* embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"http://www.youtube.com/watch?v=%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString* html = [NSString stringWithFormat:embedHTML, videoId, frame.size.width, frame.size.height];
[self.webView loadHTMLString:html baseURL:nil];
}
希望它有所帮助。
答案 1 :(得分:0)
它不起作用的原因是Web视图基本URL设置为本地。由于我的Web视图加载本地资源(图像),我需要将其设置为本地。但这意味着我无法加载在线资源(YouTube视频)。无论是其中一个。
答案 2 :(得分:0)
这是问题的真正解决方案:
“在 youtube 上观看”链接有一个 target = open in new window 属性,为了规避这一点,这里是相关代码:
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
return nil;
}
迅速:
extension YoutubeView: WKUIDelegate {
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if !(navigationAction.targetFrame?.isMainFrame ?? false) {
webView.load(navigationAction.request)
}
return nil
}
}