美文网首页
WKWebView 调用 evaluateJavaScript

WKWebView 调用 evaluateJavaScript

作者: yscGr | 来源:发表于2019-09-29 20:24 被阅读0次

HTML 代码

functionpayResult(str){

     if(str =='true'){ 

      ...     

   }

 }

OC 代码

NSString * jsStr = [NSString stringWithFormat:@"ocContent('%@','%@')",@"参数1",@"参数2"];

 [_wkWebView evaluateJavaScript:jsStr completionHandler:^(id_Nullableresult,NSError*_Nullableerror) {

}];

这么写的过程中总是崩溃,原因是js中可能有alert,但是oc中的方法没有进行操作,方法如下:

- (void)webView:(WKWebView*)webView runJavaScriptAlertPanelWithMessage:(NSString*)message initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void(^)(void))completionHandler{

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:message message:message preferredStyle:UIAlertControllerStyleAlert];

    [alertaddAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        completionHandler();

    }]];

    [self presentViewController:alert animated:YES completion:nil];

}

注:感谢 

https://juejin.im/post/5a952cd85188257a6e405b9d  

https://www.jianshu.com/p/e93d8f5ea466

相关文章

网友评论

      本文标题:WKWebView 调用 evaluateJavaScript

      本文链接:https://www.haomeiwen.com/subject/dipoectx.html