ios js 遇到的一些崩溃 2020-04-02

2020-04-02  本文已影响0人  iOS打怪升级

崩溃堆栈:was not called

0  CoreFoundation!__exceptionPreprocess + 0xdc
1  libobjc.A.dylib!objc_exception_throw + 0x34
2  CoreFoundation!+[NSException raise:format:] + 0x68
3  WebKit!WebKit::CompletionHandlerCallChecker::~CompletionHandlerCallChecker() + 0x8c


andle Exception:
04-01 10:46:51.353 [crashreportsdk]     Exception Name: NSInternalInconsistencyException
04-01 10:46:51.353 [crashreportsdk]     Exception Reason: Completion handler passed to -[CustomWkWebViewController webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:] was not called

#pragma mark - WKUIDelegate
//! alert(message)
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        completionHandler();
    }];
    [alertController addAction:cancelAction];
    UIViewController * currentVC = [HQTabBarRootViewController currentWidowsViewController];
     
    [self presentViewController:alertController animated:YES completion:nil];
}

//! confirm(message)
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(NO);
    }];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(YES);
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:confirmAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

//! prompt(prompt, defaultText)
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *))completionHandler
{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:prompt message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = defaultText;
    }];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(alertController.textFields[0].text);
    }];
    [alertController addAction:confirmAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

说明: [self presentViewController:alertController animated:YES completion:nil];
这里的present 的时候需要判断一下,否则可能会导致崩溃 runJavaScriptAlert 等代理方法显示 not called ,比如当前代码执行的时候,已经有一个alert 弹窗在显示了,此时你的present 就不会成功,那么该代理方法最终就不会有completionHandler 的调用,那么就会崩溃提示没有调用该代理方法
解决方法就是判断用当前的顶级控制器去present alert , 而不是简单的self

html 脚本参考:会触发ios 里面的WKUIDelegate 代理调用


    function alert_func()
    {
        window.alert("alert test hahah")
    }

    function confirm_func()
    {
        window.confirm("confirm ok")
    }

    function prompt_func()
    {
        window.prompt("prompt_func ok","请输入文本")
    }

有用的参考

上一篇 下一篇

猜你喜欢

热点阅读