<script language="javascript">
alert("Hell! UIWebView!");
</script>
我可以在我的UIWebView中看到UIWebView警告消息,但是我能处理这种情况吗?
更新:
我正在将一个网页加载到我的UIWebView中:
- (void)login {
NSString *requestText = [[NSString alloc] initWithFormat: @"%@?user=%@&password=%@", DEFAULT_URL, user.name, user.password]; // YES, I'm using GET request to send password :)
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:requestText]];
[webView loadRequest:request];
}
目标页面包含一个JS。如果用户名或密码不正确,此JS将显示警报。我无法接触到它的消息来源。我想在我的UIWebViewDelegate.里面处理它
发布于 2009-10-15 02:25:13
如果你所说的“包含一个闪光灯”是指你要加载到你的网页视图中有一个Adobe电影,恐怕你运气不好。移动Safari不支持Flash,而且很可能永远也不支持Flash。
在一般情况下,如果您希望运行在web视图中的JavaScript与承载它的本地应用程序进行通信,则可以加载假URL(例如:JavaScript)。它将触发webView:shouldStartLoadWithRequest:navigationType:
委托方法。在该方法中,检查请求,如果正在加载的URL是这些内部通信之一,则在应用程序中触发适当的操作,并返回NO
。
发布于 2009-10-18 19:49:10
更好的解决这个问题的方法是为方法创建一个UIWebView类别
webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:
这样您就可以以任何您想要的方式处理警报事件。我这样做是因为我不喜欢UIWebView的默认行为,因为它将源代码的文件名放在UIAlertView标题中。类别看起来是这样的,
@interface UIWebView (JavaScriptAlert)
- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame;
@end
@implementation UIWebView (JavaScriptAlert)
- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
UIAlertView* dialogue = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[dialogue show];
[dialogue autorelease];
}
@end
发布于 2014-10-14 12:23:07
这似乎起到了作用:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
JSContext *ctx = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
ctx[@"window"][@"alert"] = ^(JSValue *message) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"JavaScript Alert" message:[message toString] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
};
}
注意:只在iOS 8上测试。
https://stackoverflow.com/questions/1565102
复制相似问题