WKWebView evaluateJavaScript不适用于youtube嵌入式视频url

WKWebView evaluateJavaScript不适用于youtube嵌入式视频url,javascript,ios,swift,youtube,wkwebview,Javascript,Ios,Swift,Youtube,Wkwebview,我已将youtube嵌入视频加载到WKWebView: 然后在触发WKWebView didFinishNavigation事件后,我调用: func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { webView.evaluateJavaScript("document.querySelector('video').play()", completionHandler: { (result, erro

我已将youtube嵌入视频加载到WKWebView:

然后在触发WKWebView didFinishNavigation事件后,我调用:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

webView.evaluateJavaScript("document.querySelector('video').play()", completionHandler: { (result, error) in
    if let r = result {
        print(r)
    }
    if let e = error {
        print(e)
    }
  })
}
但是,没有执行javascript命令,错误和结果都为零

当我在Chrome开发者工具中执行相同的javascript命令时,它成功地播放了视频,并通过调用“play()”和“pause()”暂停了视频

我不知道WKWebView内部发生了什么,有什么想法吗?
谢谢

使用UIWebView而不是WKWebView,并使用此代码播放youtube视频

#pragma mark - Embed Video

- (UIWebView *)embedVideoYoutubeWithURL:(NSString *)urlString andFrame:(CGRect)frame {
    NSString *videoID = [self extractYoutubeVideoID:urlString];

    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/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];
    UIWebView *videoWebView = [[UIWebView alloc] initWithFrame:frame];
    [videoWebView loadHTMLString:html baseURL:nil];

    return videoWebView;
}

/**
 @see https://devforums.apple.com/message/705665#705665
 extractYoutubeVideoID: works for the following URL formats:

 www.youtube.com/v/VIDEOID
 www.youtube.com?v=VIDEOID
 www.youtube.com/watch?v=WHsHKzYOV2E&feature=youtu.be
 www.youtube.com/watch?v=WHsHKzYOV2E
 youtu.be/KFPtWedl7wg_U923
 www.youtube.com/watch?feature=player_detailpage&v=WHsHKzYOV2E#t=31s
 youtube.googleapis.com/v/WHsHKzYOV2E
*/

- (NSString *)extractYoutubeVideoID:(NSString *)urlYoutube {
    NSString *regexString = @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)";
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:NSRegularExpressionCaseInsensitive error:&error];
    NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:urlYoutube options:0 range:NSMakeRange(0, [urlYoutube length])];

    if(!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) {
        NSString *substringForFirstMatch = [urlYoutube substringWithRange:rangeOfFirstMatch];

        return substringForFirstMatch;
    }

    return nil;
}
#pragma标记-嵌入视频
-(UIWebView*)嵌入视频YouTubeWithUrl:(NSString*)urlString和frame:(CGRect)frame{
NSString*videoID=[self-extractYoutubeVideoID:urlString];
NSString*embedHTML=@”\
\
\
身体{\
背景色:透明\
颜色:白色\
}\
\
\
\
";
NSString*html=[NSString stringWithFormat:embedHTML,videoID,frame.size.width,frame.size.height];
UIWebView*videoWebView=[[UIWebView alloc]initWithFrame:frame];
[videoWebView加载HTMLSTRING:html基URL:nil];
返回videoWebView;
}
/**
@看https://devforums.apple.com/message/705665#705665
extractYoutubeVideoID:适用于以下URL格式:
www.youtube.com/v/VIDEOID
www.youtube.com?v=VIDEOID
www.youtube.com/watch?v=WHsHKzYOV2E&feature=youtu.be
www.youtube.com/watch?v=WHsHKzYOV2E
youtu.be/KFPtWedl7wg_U923
www.youtube.com/watch?feature=player_detailpage&v=WHsHKzYOV2E#t=31s
youtube.googleapis.com/v/WHsHKzYOV2E
*/
-(NSString*)ExtractYouTube设备ID:(NSString*)urlYoutube{

NSString*regexString=@“(?OK,终于找到了它不工作的原因

如果您也在为“为什么我的javascript不能在WKWebView中工作”而苦恼,下面是一个简单的方法来找出原因:

1.在桌面上打开Safari,首选项->高级,启用“在菜单栏中显示开发者菜单”

2.在Safari菜单中,开发->模拟器,连接iPhone模拟器

3.现在您可以在Safari中查看网页html和脚本,在控制台输入区域中,尝试运行不同的javascript以查看哪一个可用。

4.然后调用“evaluateJavaScript”中的一个

从我的案例来看,它不起作用的原因是,在普通web浏览器和WKWebView之间,嵌入式网页的呈现方式不同。我以浏览器脚本为例,它不起作用

在Safari调试器的帮助下,您可以看到WKWebView内部的真实情况,这非常酷

如果你遇到同样的问题,希望它能帮助你。
谢谢!

我不能…我的项目已修复为仅使用wkwebview…不过谢谢
#pragma mark - Embed Video

- (UIWebView *)embedVideoYoutubeWithURL:(NSString *)urlString andFrame:(CGRect)frame {
    NSString *videoID = [self extractYoutubeVideoID:urlString];

    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/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];
    UIWebView *videoWebView = [[UIWebView alloc] initWithFrame:frame];
    [videoWebView loadHTMLString:html baseURL:nil];

    return videoWebView;
}

/**
 @see https://devforums.apple.com/message/705665#705665
 extractYoutubeVideoID: works for the following URL formats:

 www.youtube.com/v/VIDEOID
 www.youtube.com?v=VIDEOID
 www.youtube.com/watch?v=WHsHKzYOV2E&feature=youtu.be
 www.youtube.com/watch?v=WHsHKzYOV2E
 youtu.be/KFPtWedl7wg_U923
 www.youtube.com/watch?feature=player_detailpage&v=WHsHKzYOV2E#t=31s
 youtube.googleapis.com/v/WHsHKzYOV2E
*/

- (NSString *)extractYoutubeVideoID:(NSString *)urlYoutube {
    NSString *regexString = @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)";
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:NSRegularExpressionCaseInsensitive error:&error];
    NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:urlYoutube options:0 range:NSMakeRange(0, [urlYoutube length])];

    if(!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) {
        NSString *substringForFirstMatch = [urlYoutube substringWithRange:rangeOfFirstMatch];

        return substringForFirstMatch;
    }

    return nil;
}