IOS app webview里面怎么监听跨域iframe里面video的播放事件?

IOS app webview里面怎么监听跨域iframe里面video的播放事件,点击播放视频的时候,打开的是自己的播放器

阅读 2.6k
1 个回答
  1. webView 通过 userContentController 注册一个MSInfo的自定义消息;
  2. 前端需要在播放视频按钮被点击时候捕捉这个点击事件并且调用 window.webkit.messageHandlers.iOSIOV.postMessage(['MSInfo','{key:value}]) 把这个事件发给iOS端
  3. iOS端通过didReceiveScriptMessage:接受事件

前提是前端可以捕捉到点击事件

WKUserContentController* userContentController =[[WKUserContentController alloc]init];
//注册message
[userContentController addScriptMessageHandler:self name:@"MSInfo"];

WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc]init];

configuration.userContentController = userContentController;
configuration.preferences.javaScriptEnabled = YES;

WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 500) configuration:configuration];
 
//前端通过这种方法发消息
//window.webkit.messageHandlers.iOSIOV.postMessage(['MSInfo','{key:value}'])

//接受从前端收到的消息;webView 的代理方法
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    
    if ([message.name isEqualToString:@"MSInfo"]) {
               NSString *para = @"{}";
                //调用前端方法传值
                [message.webView evaluateJavaScript:@"JSCallBack(%@);",para] completionHandler:^(id _Nullable dic, NSError * _Nullable error) {
                   
                }];
              
            }
        }
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏