贴代码:

@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 UIKit
import WebKit //导入WebKit  WKWebView应该是用Webkit内核
 
class ViewControllerUIViewController,WKNavigationDelegate,WKUIDelegate {
 
    var wkBrowser:WKWebView!
     
    override func viewDidLoad() {
        super.viewDidLoad()
        self.wkBrowser WKWebView(frameself.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(htmlbaseURLnil)
        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 ViewController
extension wkNavigationDelegate {
    func webView(webViewWKWebViewdidFailNavigation navigationWKNavigation!, withError errorNSError) {
        NSLog(error.debugDescription)
    }
     
    func webView(webViewWKWebViewdidFailProvisionalNavigation navigationWKNavigation!, withError errorNSError) {
        NSLog(error.debugDescription)
    }
}
 
private typealias wkUIDelegate ViewController
extension wkUIDelegate {
    //HTML页面Alert出内容
    func webView(webViewWKWebViewrunJavaScriptAlertPanelWithMessage messageStringinitiatedByFrame frameWKFrameInfocompletionHandler: () -> Void) {
        let ac UIAlertController(titlewebView.titlemessagemessagepreferredStyleUIAlertControllerStyle.Alert)
        ac.addAction(UIAlertAction(title"Ok"styleUIAlertActionStyle.Cancelhandler: { (a) -> Void in
            completionHandler()
        }))
         
        self.presentViewController(acanimatedtruecompletionnil)
    }
     
    //HTML页面弹出Confirm时调用此方法
    func webView(webViewWKWebViewrunJavaScriptConfirmPanelWithMessage messageStringinitiatedByFrame frameWKFrameInfocompletionHandler: (Bool) -> Void) {
        let ac UIAlertController(titlewebView.titlemessagemessagepreferredStyleUIAlertControllerStyle.Alert)
        ac.addAction(UIAlertAction(title"Ok"styleUIAlertActionStyle.Defaulthandler:
            { (ac) -> Void in
                completionHandler(true)  //按确定的时候传true
        }))
         
        ac.addAction(UIAlertAction(title"Cancel"styleUIAlertActionStyle.Cancelhandler:
            { (ac) -> Void in
                completionHandler(false)  //取消传false
        }))
         
        self.presentViewController(acanimatedtruecompletionnil)
    }
}

如果你访问的页面的协议是https那么要在info.list同添加以下Key:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

具体可参考:https://lvwenhan.com/ios/460.html

链接:

iOS Webview 实现修改javascript confirm 和 alert

UIWebView 屏蔽 alert警告框

WKWebView捕获HTML弹出的Alert和Confirm

iOS Webview 实现修改javascript confirm 和 alert的更多相关文章

  1. 修改js confirm alert 提示框文字的简单实例

    修改js confirm alert 提示框文字的简单实例: <!DOCTYPE html> <html> <head lang="en"> & ...

  2. Android WebView中的JavaScript代码使用

    在WebView中使用JavaScript 如果你想要载入的页面中用了JavaScript,你必须为你的WebView使能JavaScript. 一旦使能之后,你也可以自己创建接口在你的应用和Java ...

  3. Android WebView使用与JavaScript使用

    WebView基本使用 WebView是View的一个子类,可以让你在activity中显示网页. 可以在布局文件中写入WebView:比如下面这个写了一个填满整个屏幕的WebView: <?x ...

  4. iOS webview加载时序和缓存问题总结

    iOS webView的加载时序 UIWebView加载顺序: - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSU ...

  5. asp.net后台操作javascript:confirm返回值

    在asp.net中使用confirm可以分为两种: 1.没有使用ajax,confirm会引起也面刷新 2.使用了ajax,不会刷新 A.没有使用ajax,可以用StringBuilder来完成. ( ...

  6. UWP 在 WebView 中执行 JavaScript 代码(用于模拟用户输入等) - walterlv

    原文:UWP 在 WebView 中执行 JavaScript 代码(用于模拟用户输入等) - walterlv UWP 在 WebView 中执行 JavaScript 代码(用于模拟用户输入等) ...

  7. 仿写confirm和alert弹框

    在工作中,我们常常会遇到原生的样式感觉比较丑,又和我们做的项目风格不搭.于是就有了仿写原生一些组件的念头,今天我就带大家仿写一下confirm和alert样式都可以自己修改. 有些的不好的地方请指出来 ...

  8. JavaScript confirm 自定义风格及功能实现

    在网上找了一些弹窗插件,例如bootbox, 功能和动画效果都做的很好,但是很难自定义样式. 项目需要,Google相关方法后写了一个Demo, 没有JavaScript confirm切断线程的功能 ...

  9. javascript confirm()函数的用法

    javascript confirm()函数的用法 confirm():确认消息对话框.用于允许用户做选择的动作.弹出的对话框中包含一确定按钮和一取消按钮. confirm(str) 参数说明: st ...

随机推荐

  1. cessss

    [文字] 关注1啊啊啊啊啊 点击关注微信 点击关注微信2 点击关注微信3 关注2啊啊啊啊啊啊啊啊啊啊啊 关注3啊啊啊啊啊啊啊啊 关注4啊啊啊啊啊啊啊啊 关注5啊啊啊啊啊啊啊啊 关注6啊啊啊啊啊啊啊啊啊 ...

  2. 弹出页面遮罩层,以及web端和移动端阻止遮罩层的滑动。

    最近项目遇到了遮罩层的一些问题,总结一下: 弹出遮罩层 遮罩层弹出有非常多的方法,这里只写出本人用的代码,使用jq操作dom的方法进行实现的. <style>.box{position:a ...

  3. 在其他系统Iframe中显示SharePoint 页面

    前段时间在做一个项目,要求将SharePoint 的 OWA(Office Web Apps)中的文档显示页面嵌入到另外一个OA系统中,提供给用户可以通过浏览器查看SharePoint文档的能力. 嵌 ...

  4. 判断用户输入的银行卡号是否正确--基于Luhn算法的格式校验

    开发中,有时候,为了打造更好的用户体验,同时减轻服务器端的压力,需要对于一些如,手机号码,银行卡号,身份证号码进行格式校验 下面是判断银行卡号输入是否正确的代码(基于Luhn算法的格式校验): iOS ...

  5. 在TableView上添加悬浮按钮

    如果直接在TableVIewController上贴Button的话会导致这个会随之滚动,下面解决在TableView上实现位置固定悬浮按钮的两种方法: 1.在view上贴tableView,然后将悬 ...

  6. DrawerLayout的openDrawer()和closeDrawer()方法

    如下代码 DrawerLayout mdrawerLayout; Button btn; ------------------------------------------------------- ...

  7. Lucene搜索方式大合集

    package junit; import java.io.File; import java.io.IOException; import java.text.ParseException; imp ...

  8. 让我们来谈谈JDBC

    1.JDBC     1)JDBC简介         - JDBC就是Java中连接数据库方式         - 我们可以通过JDBC来执行SQL语句.       2)获取数据库连接   - j ...

  9. windows下用QTwebkit解析html

    环境 windows7 + VS2010 + QT5.2_opengl 配置开发环境 1.安装VS2010 2.安装QT 5.2 QT网站:http://qt-project.org/download ...

  10. git review & devops过程

    自己搭建的devops环境是gitlab/gerrit/jenkins 1. 首先自己checkout一个自己的代码分支,一般不要在master上做直接修改 2. 修改后git add file,   ...