WKWebView 调用 evaluateJavaScript

2019-09-29  本文已影响0人  yscGr

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

上一篇下一篇

猜你喜欢

热点阅读