問題如下圖
解決方案
UIWebView 屏蔽 alert警告框
如果是網(wǎng)頁內(nèi)容里面的alert,我們可以等網(wǎng)頁加載完畢,也就是在webViewDidFinishLoad中執(zhí)行下面的js代碼,就可以屏蔽alert了
myWebView stringByEvaluatingJavaScriptFromString:@"window.alert=null;"];
但上面的方法對(duì)于網(wǎng)頁onLoad事件里面的alert就不起作用了
解決方法就是給UIWebView添加一個(gè)類別:
給工程添加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
其實(shí)runJavaScriptAlertPanelWithMessage和runJavaScriptConfirmPanelWithMessage是WebUIDelegate的成員函數(shù),詳情可以查閱WebUIDelegate的幫助文檔