【问题标题】:Link "Watch on youtube" doesn't work from iOS UIWebView?链接“在 youtube 上观看”在 iOS UIWebView 中不起作用?
【发布时间】:2013-05-17 16:30:09
【问题描述】:

我已经嵌入了一个 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 中也做过同样的事情,而且效果很好。

谢谢

【问题讨论】:

    标签: ios uiwebview youtube-api


    【解决方案1】:

    这是问题的真正解决方案:

    “在 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
        }
    }
    

    【讨论】:

      【解决方案2】:

      它不起作用的原因是Web视图Base URL设置为本地。由于我的 web 视图加载本地资源(图像),我需要将其设置为本地。但这意味着我无法加载在线资源(youtube 视频)。一个或另一个。

      【讨论】:

      • 这似乎不是问题,您只能在加载 HTML 字符串时设置基本 URL,加载 youtube 嵌入 URL 时无法点击此链接
      【解决方案3】:

      据我所知,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];
      }
      

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2013-05-14
        • 2018-10-03
        • 1970-01-01
        • 1970-01-01
        • 2011-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多