贴代码:

@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. Java--Callable与返回值future

    package com; import java.util.concurrent.*; /** * Created by yangyu on 16/11/28. */ /** * Callable a ...

  2. 对hashcode、equals的理解

    1.首先hashcode和equals都是java每个对象都存在的方法,因为他们两是Object的方法. 2.hashcode方法默认返回的是该对象内存地址的哈希码,然而你会发现,Object类中没有 ...

  3. 企业管理咨询Interview Checklist

    企业管理咨询Interview Checklist 一. 企业战略 1. 您对公司所处行业的看法如何? 2. 请您介绍一下公司的发展历程,主要业务开展状况及核心竞争力.关键成功因素有哪些? 3. 在您 ...

  4. javascript 模式(1)——代码复用

    程序的开发离不开代码的复用,通过代码复用可以减少开发和维护成本,在谈及代码复用的时候,会首先想到继承性,但继承并不是解决代码复用的唯一方式,还有其他的复用模式比如对象组合.本节将会讲解多种继承模式以实 ...

  5. Gulp如何编译sass

    Gulp 是一个自动化工具,前端开发者可以使用它来处理常见任务: 1.搭建web服务器 2.文件保存时自动重载浏览器 3.使用预处理器如Sass.LESS 4.优化资源,比如压缩CSS.JavaScr ...

  6. [deviceone开发]-小草用户分享的Listview停靠的示例

    一.简介 这个例子展示了Listview的多模板,上拉下拉功能,也实现了上下滑动第二行工具栏的停靠功能,值得参考 二.效果图 三.相关下载 https://github.com/do-project/ ...

  7. jQuery siteslider 动画幻灯片

    在线实例 效果一 效果二 使用方法 <div class="container demo-1">             <div id="slider ...

  8. iOS之数据持久化方案

    概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方案: plist文件(属性列表) ...

  9. iOS切图文件的命名规范

    万能公式:

  10. RoboGuice 3.0 (一)入坑篇

    RoboGuice是什么? 一个Android上的依赖注入框架. 依赖注入是什么? 从字面理解,这个框架做了两件事情,第一是去除依赖,第二是注入依赖.简单理解就是,将对象的初始化委托给一个容器控制器, ...