崩溃提示
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");
}
}