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

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

解决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];

}

解决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];

}

解决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];

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前言: web页面和app的直接的交互是很常见的东西,在ios8之前,用的是uiwebview,但是在ios8之后...
    qingchen91阅读 8,114评论 6 25
  • 导语 WKWebView 是苹果在 WWDC 2014 上推出的新一代 webView 组件,用以替代 UIKit...
    Jecky丶阅读 12,766评论 2 22
  • 1、WKWebView 白屏问题WKWebView 自诩拥有更快的加载速度,更低的内存占用,但实际上 WKWebV...
    iosRn阅读 6,365评论 1 10
  • 你的梦想是什么? 就是大脑里你想象的画面,有可能是你的事业未来的样子,有可能是你老了的样子,有可能是你家人的状态。...
    爱孕安馨刘建民阅读 2,817评论 0 1
  • 遇知心人是我的幸運,若遇到人渣就當上一課,令到自己成長,你沒有失去而是得到,失去的卻是他們,因為他們失去了獨一無二...
    小丫_丫阅读 1,310评论 0 0

友情链接更多精彩内容