iOS WKWebView崩溃闪退 Completion handler passed to -[XXWebViewVC webView:runJavaScriptAlertPanelWithM…

作者 : 开心源码 本文共3055个字,预计阅读时间需要8分钟 发布时间: 2022-05-13 共311人阅读

崩溃提醒

Completion handler passed to -[WebViewVC webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:] was not called

起因分析

在WKWebView的WKUIDelegate回调方法中,(void (^)(void))completionHandler和(void (^)(BOOL))completionHandler无论YES or No,必需执行。假如当前WebViewVC划走,completionHandler还未执行,那么就会报这个崩溃提醒(我推迟了completionHandler的执行时间,复现了崩溃现象)。

处理方法

直接上代码,不需要额外添加属性,在WKUIDelegate几个用到(void (^)(BOOL))completionHandler及(void (^)(void))completionHandler的回调方法中加上判断即可以处理。

#pragma mark -- WKUIDelegate- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{    if ([self.navigationController visibleViewController] != self)    {        completionHandler();        return;    }        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];    [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { completionHandler();    }]];    if ([self.navigationController visibleViewController] == self)    {        [self presentViewController:alertController animated:YES completion:nil];    }    else    {        completionHandler();    }}// 显示两个按钮,通过completionHandler回调判断客户点击确实定还是取消按钮- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{    if ([self.navigationController visibleViewController] != self)    {        completionHandler(NO);        return;    }        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message message:nil preferredStyle:UIAlertControllerStyleAlert];    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {                completionHandler(YES);    }]];    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {                completionHandler(NO);    }]];        if ([self.navigationController visibleViewController] == self)    {        [self presentViewController:alertController animated:YES completion:nil];    }    else    {        completionHandler(NO);    }}// 显示一个带有输入框和一个确定按钮的,通过completionHandler回调客户输入的内容- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler{        if ([self.navigationController visibleViewController] != self)    {        completionHandler(@"error");        return;    }        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:nil preferredStyle:UIAlertControllerStyleAlert];    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {            }];    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {                completionHandler(alertController.textFields.lastObject.text);    }]];        if ([self.navigationController visibleViewController] == self)    {        [self presentViewController:alertController animated:YES completion:nil];    }    else    {        completionHandler(@"error");    }}
说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » iOS WKWebView崩溃闪退 Completion handler passed to -[XXWebViewVC webView:runJavaScriptAlertPanelWithM…

发表回复