贴代码:

@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. 【夯实PHP系列】购物车代码说明PHP的匿名函数

    1. 定义:匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数.最经常用作回调函数(callback)参数的值.当然,也有其它应 ...

  2. 十一个行为模式之迭代器模式(Iterator Pattern)

    定义: 提供一种方法来访问聚合对象,而不用暴露这个对象的内部表示.使得存储和遍历两个职责相互分离,提高系统的可扩展性. 结构图: Iterator:抽象迭代器类,定义了访问和遍历元素的接口,例如:ne ...

  3. PHP预定义变量

    * PHP预定义 * 预定义变量 * $_GET - 接收客户端以请求类型为GET方法发送的数据内容 * $_POST - 接收客户端以请求类型为POST方法发送的数据内容 * $_REQUEST - ...

  4. 【grunt第三弹】grunt在前端实际项目中的应用

    前言 [grunt第二弹]30分钟学会使用grunt打包前端代码(02) [grunt第一弹]30分钟学会使用grunt打包前端代码 经过前两次的学习,我们了解了grunt打包的一些基础知识,对于压缩 ...

  5. 编写可维护的CSS

    在参与规模庞大.历时漫长且参与人数众多的项目时,所有开发者遵守如下规则极为重要: 保持 CSS 便于维护 保持代码清晰易懂 保持代码的可拓展性 为了实现这一目标,我们要采用诸多方法. 本文档第一部分将 ...

  6. 你所不知的 CSS ::before 和 ::after 伪元素用法

    CSS 有两个说不上常用的伪类 :before 和 :after,偶尔会被人用来添加些自定义格式什么的,但是它们的功用不仅于此.前几天发现了 Creative Link Effects 这个非常有意思 ...

  7. 那些过目不忘的H5页面

    原文链接:http://isux.tencent.com/great-mobile-h5-pages.html 从引爆朋友圈的H5小游戏<围住神经猫>,到颠覆传统广告的大众点评H5专题页& ...

  8. JavaScript区分click事件和mousedown(mouseup、mousemove)方法

    在前端开发工作中,会遇到这样问题:针对同一个dom元素,即希望为它绑定click事件,又想该元素可以允许拖拽的效果.而使用拖拽的效果,我们一般就会用到mousedown,mousemove和mouse ...

  9. Android 缓存

    1.Android缓存机制&一个缓存框架推荐 http://blog.csdn.net/shakespeare001/article/details/51695358 2.ASimpleCac ...

  10. ListView setOnItemClickListener无效原因分析

    前言 最近在做项目的过程中,在使用listview的时候遇到了设置item监听事件的时候在没有回调onItemClick 方法的问题.我的情况是在item中有一个Button按钮.所以不会回调.上百度 ...