博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIWebView 屏蔽或者修改 alert警告框
阅读量:4973 次
发布时间:2019-06-12

本文共 4105 字,大约阅读时间需要 13 分钟。

如果是网页内容里面的alert,我们可以等网页加载完毕,也就是在webViewDidFinishLoad中执行下面的js代码,就可以屏蔽alert了 [myWebView stringByEvaluatingJavaScriptFromString:@"window.alert=null;"];但上面的方法对于网页onLoad事件里面的alert就不起作用了解决方法就是给UIWebView添加一个类别:给工程添加JavaScriptAlert.h@interface UIWebView (JavaScriptAlert) - (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame;- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame;@end给工程添加JavaScriptAlert.m文件@implementation UIWebView (JavaScriptAlert)- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {    /*     UIAlertView* dialogue = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil];     [dialogue show];     [dialogue autorelease];     */}- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame{   return NO;}@end其实runJavaScriptAlertPanelWithMessage和runJavaScriptConfirmPanelWithMessage是UIWebDelegate的成员函数,详情可以查阅UIWebDelegate的帮助文档

 

 

 

 JavaScript中有三种形式的弹出框,alert,confirm,prompt;

修改alert内容的话:

捕捉 JavaScript alert()   JavaScript confirm()  JavaScript prompt()这三种形式的

给工程添加JavaScriptAlert.h

@interface UIWebView (JavaScriptAlert) - (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame;- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame; - (NSString *)webView:(UIWebView *)sender runJavaScriptTextInputPanelWithPrompt:(NSString *)str defaultText:(NSString *)mes initiatedByFrame:(WebFrame *)frame; @end给工程添加JavaScriptAlert.m文件@implementation UIWebView (JavaScriptAlert)

static BOOL diagStat = NO;

1、

- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame

{
     NSLog(@"javascript alert : %@",message);
    UIAlertView *confirmDiag = [[UIAlertView alloc] initWithTitle:@"沃德餐饮提醒您" message:message delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel") otherButtonTitles:NSLocalizedString(@"OK", @"OK"), nil];
    [confirmDiag show];
    
    while (confirmDiag.hidden == NO && confirmDiag.superview != nil)
        
        [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01f]];
        
    [confirmDiag release];
    return diagStat;
}

 

 2、

- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {

    UIAlertView* customAlert = [[UIAlertView alloc] initWithTitle:@"沃德餐饮提醒您" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [customAlert show];
    [customAlert autorelease];  
}

 

3、

- (NSString *)webView:(UIWebView *)sender runJavaScriptTextInputPanelWithPrompt:(NSString *)str defaultText:(NSString *)mes initiatedByFrame:(WebFrame *)frame
{
    NSString *subTitle = [NSString stringWithFormat:@"%@\n\n",str];
    UIAlertView *promptDiag = [[UIAlertView alloc] initWithTitle:@"沃德餐饮提醒您" message:subTitle delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel") otherButtonTitles:NSLocalizedString(@"OK", @"OK"), nil];
    
    UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(10, 80, 265, 30)];
    //nameField.borderStyle =  UITextBorderStyleRoundedRect;
    nameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [nameField becomeFirstResponder];
    nameField.backgroundColor = [UIColor whiteColor];
    [promptDiag addSubview:nameField];
    [nameField release];
    
    [promptDiag show];
    
    while (promptDiag.hidden == NO && promptDiag.superview != nil)
        
        [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01f]];
    
    [promptDiag release];
    if(diagStat)
    {
        return nameField.text;
    }
    return mes;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        diagStat = NO;
    } 
    else if (buttonIndex == 1) 
    {
        diagStat = YES;
    } 
}

 

转载于:https://www.cnblogs.com/KingQiangzi/p/4043293.html

你可能感兴趣的文章
Fireworks基本使用
查看>>
Java基础常见英语词汇
查看>>
nginx启动、关闭命令、重启nginx报错open() "/var/run/nginx/nginx.pid" failed
查看>>
BZOJ 3097 Hash Killer I
查看>>
UINavigationController的视图层理关系
查看>>
html阴影效果怎么做,css 内阴影怎么做
查看>>
BZOJ1026: [SCOI2009]windy数
查看>>
样板操作数
查看>>
组件:slot插槽
查看>>
Nginx配置文件nginx.conf中文详解(转)
查看>>
POJ 1308 Is It A Tree?(并查集)
查看>>
N进制到M进制的转换问题
查看>>
springIOC第一个课堂案例的实现
查看>>
求输入成绩的平均分
查看>>
php PDO (转载)
查看>>
wordpress自动截取文章摘要代码
查看>>
[置顶] 一名优秀的程序设计师是如何管理知识的?
查看>>
highcharts 图表实例
查看>>
highcharts曲线图
查看>>
extjs动态改变样式
查看>>