iOS Webview 实现修改javascript confirm 和 alert
贴代码:
@interface UIWebView (JavaScriptAlert)
-(void) webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame;
- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame;
@end @implementation UIWebView (JavaScriptAlert) - (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame {
UIAlertView* customAlert = [[UIAlertView alloc] initWithTitle:@"助手提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[customAlert show];
}
static BOOL diagStat = NO;
static NSInteger bIdx = -;
- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame {
UIAlertView *confirmDiag = [[UIAlertView alloc] initWithTitle:@"助手提示"
message:message
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定", nil nil]; [confirmDiag show];
bIdx = -; while (bIdx==-) {
//[NSThread sleepForTimeInterval:0.2];
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1f]];
}
if (bIdx == ){//取消;
diagStat = NO;
}
else if (bIdx == ) {//确定;
diagStat = YES;
}
return diagStat;
} - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
bIdx = buttonIndex;
} @end
关于WKWebView:
之前用WebView装载一个网页时,弹出Alert时会显示网址,由于不想把网址暴露给用户这样显示就不是很友好了。UIWebView文档内没有找到可以捕获这类信息的API。GOOGLE了下发现了WKWebView组件,WKWebView是IOS8新推出的组件,目的是给出一个新的高性能的 Web View 解决方案,摆脱过去 UIWebView 的老旧笨重特别是内存占用量巨大的问题。以下为示例代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
//// ViewController.swift// KenWKWebView//// Created by KenNgai on 10/10/15.// Copyright © 2015 IT. All rights reserved.//import UIKitimport WebKit //导入WebKit WKWebView应该是用Webkit内核class ViewController: UIViewController,WKNavigationDelegate,WKUIDelegate { var wkBrowser:WKWebView! override func viewDidLoad() { super.viewDidLoad() self.wkBrowser = WKWebView(frame: self.view.frame) //self.wkBrowser.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.baidu.com")!)) let html = "<html><title>Dialog</title><style type='text/css'>body{font-size:60px}</style><script type='text/javascript'>function myconfirm(){if(confirm('Star it?')){alert('Done')}}</script><body><a href=\"javascript:alert('Just Alert')\" >Alert</a><br /><a href=\"javascript:myconfirm()\">Logout</a></body></html>" self.wkBrowser.loadHTMLString(html, baseURL: nil) self.wkBrowser.navigationDelegate = self self.wkBrowser.UIDelegate = self self.view.addSubview(wkBrowser) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}//捕捉异常信息private typealias wkNavigationDelegate = ViewControllerextension wkNavigationDelegate { func webView(webView: WKWebView, didFailNavigation navigation: WKNavigation!, withError error: NSError) { NSLog(error.debugDescription) } func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) { NSLog(error.debugDescription) }}private typealias wkUIDelegate = ViewControllerextension wkUIDelegate { //HTML页面Alert出内容 func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: () -> Void) { let ac = UIAlertController(title: webView.title, message: message, preferredStyle: UIAlertControllerStyle.Alert) ac.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: { (a) -> Void in completionHandler() })) self.presentViewController(ac, animated: true, completion: nil) } //HTML页面弹出Confirm时调用此方法 func webView(webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: (Bool) -> Void) { let ac = UIAlertController(title: webView.title, message: message, preferredStyle: UIAlertControllerStyle.Alert) ac.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (ac) -> Void in completionHandler(true) //按确定的时候传true })) ac.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (ac) -> Void in completionHandler(false) //取消传false })) self.presentViewController(ac, animated: true, completion: nil) }} |

如果你访问的页面的协议是https那么要在info.list同添加以下Key:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
具体可参考:https://lvwenhan.com/ios/460.html
链接:
iOS Webview 实现修改javascript confirm 和 alert
WKWebView捕获HTML弹出的Alert和Confirm
iOS Webview 实现修改javascript confirm 和 alert的更多相关文章
- 修改js confirm alert 提示框文字的简单实例
修改js confirm alert 提示框文字的简单实例: <!DOCTYPE html> <html> <head lang="en"> & ...
- Android WebView中的JavaScript代码使用
在WebView中使用JavaScript 如果你想要载入的页面中用了JavaScript,你必须为你的WebView使能JavaScript. 一旦使能之后,你也可以自己创建接口在你的应用和Java ...
- Android WebView使用与JavaScript使用
WebView基本使用 WebView是View的一个子类,可以让你在activity中显示网页. 可以在布局文件中写入WebView:比如下面这个写了一个填满整个屏幕的WebView: <?x ...
- iOS webview加载时序和缓存问题总结
iOS webView的加载时序 UIWebView加载顺序: - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSU ...
- asp.net后台操作javascript:confirm返回值
在asp.net中使用confirm可以分为两种: 1.没有使用ajax,confirm会引起也面刷新 2.使用了ajax,不会刷新 A.没有使用ajax,可以用StringBuilder来完成. ( ...
- UWP 在 WebView 中执行 JavaScript 代码(用于模拟用户输入等) - walterlv
原文:UWP 在 WebView 中执行 JavaScript 代码(用于模拟用户输入等) - walterlv UWP 在 WebView 中执行 JavaScript 代码(用于模拟用户输入等) ...
- 仿写confirm和alert弹框
在工作中,我们常常会遇到原生的样式感觉比较丑,又和我们做的项目风格不搭.于是就有了仿写原生一些组件的念头,今天我就带大家仿写一下confirm和alert样式都可以自己修改. 有些的不好的地方请指出来 ...
- JavaScript confirm 自定义风格及功能实现
在网上找了一些弹窗插件,例如bootbox, 功能和动画效果都做的很好,但是很难自定义样式. 项目需要,Google相关方法后写了一个Demo, 没有JavaScript confirm切断线程的功能 ...
- javascript confirm()函数的用法
javascript confirm()函数的用法 confirm():确认消息对话框.用于允许用户做选择的动作.弹出的对话框中包含一确定按钮和一取消按钮. confirm(str) 参数说明: st ...
随机推荐
- cessss
[文字] 关注1啊啊啊啊啊 点击关注微信 点击关注微信2 点击关注微信3 关注2啊啊啊啊啊啊啊啊啊啊啊 关注3啊啊啊啊啊啊啊啊 关注4啊啊啊啊啊啊啊啊 关注5啊啊啊啊啊啊啊啊 关注6啊啊啊啊啊啊啊啊啊 ...
- 弹出页面遮罩层,以及web端和移动端阻止遮罩层的滑动。
最近项目遇到了遮罩层的一些问题,总结一下: 弹出遮罩层 遮罩层弹出有非常多的方法,这里只写出本人用的代码,使用jq操作dom的方法进行实现的. <style>.box{position:a ...
- 在其他系统Iframe中显示SharePoint 页面
前段时间在做一个项目,要求将SharePoint 的 OWA(Office Web Apps)中的文档显示页面嵌入到另外一个OA系统中,提供给用户可以通过浏览器查看SharePoint文档的能力. 嵌 ...
- 判断用户输入的银行卡号是否正确--基于Luhn算法的格式校验
开发中,有时候,为了打造更好的用户体验,同时减轻服务器端的压力,需要对于一些如,手机号码,银行卡号,身份证号码进行格式校验 下面是判断银行卡号输入是否正确的代码(基于Luhn算法的格式校验): iOS ...
- 在TableView上添加悬浮按钮
如果直接在TableVIewController上贴Button的话会导致这个会随之滚动,下面解决在TableView上实现位置固定悬浮按钮的两种方法: 1.在view上贴tableView,然后将悬 ...
- DrawerLayout的openDrawer()和closeDrawer()方法
如下代码 DrawerLayout mdrawerLayout; Button btn; ------------------------------------------------------- ...
- Lucene搜索方式大合集
package junit; import java.io.File; import java.io.IOException; import java.text.ParseException; imp ...
- 让我们来谈谈JDBC
1.JDBC 1)JDBC简介 - JDBC就是Java中连接数据库方式 - 我们可以通过JDBC来执行SQL语句. 2)获取数据库连接 - j ...
- windows下用QTwebkit解析html
环境 windows7 + VS2010 + QT5.2_opengl 配置开发环境 1.安装VS2010 2.安装QT 5.2 QT网站:http://qt-project.org/download ...
- git review & devops过程
自己搭建的devops环境是gitlab/gerrit/jenkins 1. 首先自己checkout一个自己的代码分支,一般不要在master上做直接修改 2. 修改后git add file, ...