美文网首页iOS开发
# WKWebView 遇到 NSInternalInconsi

# WKWebView 遇到 NSInternalInconsi

作者: WMSmile | 来源:发表于2021-06-25 17:42 被阅读0次

    WKWebView 遇到 NSInternalInconsistencyException runJavaScriptAlertPanelWithMessage: was not called

    Completion handler passed to -[xxx.BaseWebViewController webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:] was not called

    从 crash 可以看出是 WKWebView 回调函数: runJavaScriptAlertPanelWithMessage中completionHandler 没有被调用导致的。

    出现情况可能是WKWebView 退出的时候,JS刚好执行了window.alert(), alert 框可能弹不出来,completionHandler 最后没有被执行,导致 crash;另一种情况是在 WKWebView 一打开,JS就执行window.alert(),这个时候由于 WKWebView 所在的 UIViewController 出现(push或present)的动画尚未结束,alert 框可能弹不出来,completionHandler 最后没有被执行,导致 crash。

    解决办法如下:

    #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");
            
        }
        
    }
    
    

    相关文章

      网友评论

        本文标题:# WKWebView 遇到 NSInternalInconsi

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