• iOS开发 WKWebView下js的alert(),confirm(),prompt()方法无法正常执行


    1、原因说明

    • 由于安全机制,WKWebView默认对JavaScript下alert(),confirm(),prompt())做了拦截,如果要想正常使用,需要实现WKWebView的三个代理方法.

    2、解决办法

    • 2.1 解决alert方法

    - (void)webView:(WKWebView *)webView 
    runJavaScriptAlertPanelWithMessage:(NSString *)message 
    initiatedByFrame:(WKFrameInfo *)frame 
    completionHandler:(void (^)(void))completionHandler {
    
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
    
        [alertController addAction:([UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
            completionHandler();
    
        }])];
    
        [self presentViewController:alertController animated:YES completion:nil];
    
    }
    
    • 2.2 解决confirm

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

    - (void)webView:(WKWebView *)webView 
    runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt 
    defaultText:(NSString *)defaultText 
    initiatedByFrame:(WKFrameInfo *)frame 
    completionHandler:(void (^)(NSString * _Nullable))completionHandler {
    
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:prompt message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
        [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    
            textField.text = defaultText;
    
        }];
    
        [alertController addAction:([UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
            completionHandler(alertController.textFields[0].text?:@"");
    
        }])];
    
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
  • 相关阅读:
    Java匹马行天下之一顿操作猛如虎,框架作用知多少?
    ztree树应用
    动态将ASPX生成HTML网页并将网页导出PDF
    实现图片向上不停的无限滚动效果简单代码
    简单的前端正则验证用户输入的数字是否合法
    eclipse出现jdk版本更新导致无法启动
    删除所有视图 删除所有存储过程
    删除所有表的数据
    要求必须全部重复的数据sql--想了半天才写出来的
    查询树节点下的所有子节点包括根节点
  • 原文地址:https://www.cnblogs.com/CH520/p/11968408.html
Copyright © 2020-2023  润新知