ios wkwebview js alert

1.将WKWebView的WKUIDelegate设置成self。
2.将一下三个方法拷贝到项目中。

  • 解决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 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];
}
  • 解决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) {br/>completionHandler(alertController.textFields[0].text?:@"");
    }])];
    [self presentViewController:alertController animated:YES completion:nil];
    }

ios wkwebview js alert

原文地址:https://blog.51cto.com/xuguohongai/2457658

时间: 2024-08-29 05:15:32

ios wkwebview js alert的相关文章

iOS下JS与OC互相调用(二)--WKWebView 拦截URL

在第一篇文章 iOS下JS与OC互相调用(一)中讲述了使用UIWebView拦截URL的方式来处理JS与OC交互. 由于UIWebView比较耗内存,性能上不太好,而苹果在iOS 8中推出了WKWebView. 同样的用WKWebView也可以拦截URL,做JS 与OC交互.关于WKWebView与UIWebView的对比,大家请自动百度或者google. WKWebView 拦截URL WKWebView 与 UIWebView 拦截URL 的处理方式基本一样.除了代理方法和WKWebView

ios调用Html内JS alert 不能点击关闭为甚?

ios调用Html内JS alert 不能点击关闭为甚? This question gave me the most insight to the problem... Deadlock with GCD and webView The gist is that the thread handling the JS from the stringByEvaluatingJavaScriptFromString: method and the thread handling the iOS al

iOS下JS与OC互相调用(一)--UIWebView 拦截URL

http://blog.csdn.net/u011619283/article/details/52135977 最近准备把之前用UIWebView实现的JS与原生相互调用功能,用WKWebView来替换.顺便搜索整理了一下JS 与OC 交互的方式,非常之多啊.目前我已知的JS 与 OC 交互的处理方式: * 1.在JS 中做一次URL跳转,然后在OC中拦截跳转.(这里分为UIWebView 和 WKWebView两种,去年因为还要兼容iOS 6,所以没办法只能采用UIWebView来做.) *

转--iOS下JS与原生OC互相调用

iOS下JS与原生OC互相调用 引言 一直听说WKWebView比UIWebView强大许多,可是一直没有使用到,今天花了点时间看写了个例子,对其API的使用有所了解,为了日后能少走弯路,也为了让大家更容易学习上手,这里写下这篇文章来记录如何使用以及需要注意的地方. 温馨提示:本人在学习使用过程中,确实有此体会,WKWebView的确比UIWebView强大很多,与JS交互的能力显示增强,在加载速度上有所提升. WKWebView新特性 性能.稳定性.功能大幅度提升 允许JavaScript的N

[iOS]通过JS调用iOS函数时的URL编码问题

在前面的文章:[iOS]在WebApp中如何使用JS调用iOS的函数 中,提到了如何使用JS通过修改URL调用iOS的内部函数. 其中会遇到一个问题,就是编码问题,比如通过URL调用弹窗,在里面写上内容:你好汪海. 那链接大概就是这样的:http://xxx.com#ios?action=alert&param=你好汪海 但是在iOS中接收到的时候会出现中文的乱码: http://xxx.com#ios?action=alert&param=%25E6%2596%2587%25E4 遇到这

iOS WKWebview 网页开发适配指南

iOS WKWebview 网页开发适配指南 微信iOS客户端将于2017年3月1日前逐步升级为WKWebview内核,需要网页开发者提前做好网站的兼容检查和适配.如有问题,可参考文末联系方式,向我们咨询. 背景 WKWebView 是苹果在iOS 8中引入的新组件,目的是提供一个现代的支持最新Webkit功能的网页浏览控件,摆脱过去 UIWebView的老.旧.笨,特别是内存占用量巨大的问题.它使用与Safari中一样的Nitro JavaScript引擎,大大提高了页面js执行速度. 切换方

IOS Webview JS交互之事件拦截获取

最近项目中用到了webvVew,webView加载的网页中有两个按钮,要在app里面响应这两个按钮的点击事件,并接受JS传过来的参数. ios7 的时候,出来了一个新的类库叫JavaScriptCore.framework - (void)setupJsContent { //获取当前JS环境 _content = [_webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; // 打印异

C#中的js alert

假设有一函数如下private void RealDelete(){Respose.Write("<script>alert('真的要删除吗?'),</script>");其他语句}现在知道的情况是,当执行该函数时,并不是执行到Respose语句时就弹出警告框,而是在这个函数的所有语句都执行完毕后才会弹出警告,也就是事实上没有起到警示作用. C#中的js alert

iOS WKWebView 混合应用开发时代来临 - 嵌入式 JS 的调试成为催化剂

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 早在五年前,在哈尔滨大街小巷的饭馆儿里,经常听到店老板抱怨服务员不好招,钱给少了没人干! 那时还特意做了个分析,2010年倒推30年,正好是198