贴代码:

@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. 关于前期在云服务器上部署TOMCAT服务器的问题

    如果你要将你的项目放在服务器上(针对云服务器),首先你必须要学会前期部署Tomcat的准备, 第一步:安装jdk,配置环境变量 这个比较简单,配置java环境变量最为简单的方法是将java的 (../ ...

  2. springmvc添加mock json的支持

    在springmvc中 添加对服务器classPath下的json文件解析之后返回的mock功能: import java.io.FileNotFoundException; import java. ...

  3. Java基础学习总结 -- 多线程的实现

    目录: 继承Thread类 start()方法实现多线程的原理 实现Runnable接口 Thread类 与 Runnable接口 的联系与区别 多线程的实现方法: 继承Thread类 实现Runna ...

  4. 动画在webapp中的现状

    webapp的一大优势便是在view切换时候可以拥有媲美与native的动画效果,但是很多时候那只是一种想法,真正的情况却不是这样 产生此问题的原因有: ① 手机CPU烂! ② 手机显卡烂!就算四核其 ...

  5. iOS 9 强制横屏

    首先在plist 文件中 Supported interface orientations 选项 只留下一个 portrait 屏幕强制横屏 使用以下代码 self.navigationControl ...

  6. App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file

    ios进行http请求,会出现这个问题: App Transport Security has blocked a cleartext HTTP (http://) resource load sin ...

  7. React Native常用组件Image使用

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  8. HttpUrlConnection

    •HttpUrlConnection是java的标准类,继承UrlConnection类,二者都是抽象类.其对象主要通过URL的                                     ...

  9. Mac安装mysql遇到的坑

    使用XMPP前安装了mysql,小白遇到好多坑,不要见怪 mysql官网下载,选择Community中扩展名为dmg的文件 安装完成后要记住密码,重要的事情只说一遍,对了,安装时也没有偏好设置和自启动 ...

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

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