如果是网页内容里面的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; } }