赞
踩
不论使用的是UIWebView还是WKWebView,控件在加载有视频的网页时,我们想抓取网页上的视频链接,用来干其他的事,这里我演示抓取视频链接用自己自定义的视频播放器来播放。
获取视频链接的JS代码及调用方式如下,
- NSString *JSStr = @"(document.getElementsByTagName(\"video\")[0]).src";
- NSString *videoUrlStr = [self.webView stringByEvaluatingJavaScriptFromString:JSStr];
- NSLog(@"videoUrlStr == %@",videoUrlStr);
获取视频链接的JS代码及调用方式如下,
- - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
- NSString *JsStr = @"(document.getElementsByTagName(\"video\")[0]).src";
- [webView evaluateJavaScript:JsStr completionHandler:^(id _Nullable response, NSError * _Nullable error) {
- if(![response isEqual:[NSNull null]] && response != nil){
- //截获到视频地址了
- NSLog(@"response == %@",response);
- }else{
- //没有视频链接
- }
- }];
- }
播放视频
获取到视频的URL以后,我自己自定义了一个视频播放器来播放;
- NSURL *videoURLZFJ = [NSURL URLWithString:videoUrlStr];
- ZFJMediaPlayer *ZFJPlayer = [[ZFJMediaPlayer alloc]init];
- ZFJPlayer.isShowDownLoad = YES;
- ZFJPlayer.downLoadClick = ^(UIButton *downLoadBtn,NSURL *videoURL){
- NSLog(@"videoURL == %@",videoURL.absoluteString);
- };
- [ZFJPlayer showZFJMediaWithURL:videoURLZFJ];
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。