刚刚遇到一个问题,记录一下
关于WKWebView 的runJavaScriptAlertPanelWithMessage:方法崩溃
出现情况可能是WKWebView 退出的时候,JS刚好执行了window.alert(), alert 框可能弹不出来,completionHandler 最后没有被执行,导致 crash;另一种情况是在 WKWebView 一打开,JS就执行window.alert(),这个时候由于 WKWebView 所在的 UIViewController 出现(push或present)的动画尚未结束,alert 框可能弹不出来,completionHandler 最后没有被执行,导致 crash。
所以调试的时候先清楚前端在页面做了什么新操作。有可能增加一个alert就可能导致crash。当然我们代码先写好也行。
解决方法:
-(void)webView:(WKWebView*)webView runJavaScriptAlertPanelWithMessage:(NSString*)message initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void(^)(void))completionHandler{
if(/*UIViewController of WKWebView has finish push or present animation*/){
completionHandler();
return;
}
UIAlertController*alertController= [UIAlertController alertControllerWithTitle:@""message:message preferredStyle:UIAlertControllerStyleAlert];[alertController addAction:[UIAlertAction actionWithTitle:@"确认"style:UIAlertActionStyleCancel handler:^(UIAlertAction*action){completionHandler();}]];
if(/*UIViewController of WKWebView is visible*/)
[selfpresentViewController:alertController animated:YES completion:^{}];
else completionHandler();
}
网友评论