我可以在 UIWebViewDelegate 中处理警报吗? [英] Can I handle alert inside UIWebViewDelegate?

查看:18
本文介绍了我可以在 UIWebViewDelegate 中处理警报吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script language="javascript">
    alert("Hell! UIWebView!");
</script>

我可以在我的 UIWebView 中看到警报消息,但我可以处理这种情况吗?

I can see the alert message inside my UIWebView but can I handle this situation?

更新:

我正在将网页加载到我的 UIWebView 中:

I'm loading a web-page into my 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 中处理它.

The target page contain a JS. If user name or password is incorrect this JS show alert. I have not any access to its sources. I want to handle it inside my UIWebViewDelegate.

推荐答案

如果包含 Flash"是指您正在加载到 Web 视图中的页面中包含 Adob​​e Flash 电影,那么您就不走运了, 我耽心.移动版 Safari 不支持 Flash,而且很可能永远不会.

If by "contain a flash" you mean the page you're loading into your web view has an Adobe Flash movie in it, you're out of luck, I'm afraid. Mobile Safari doesn't support Flash, and most likely never will.

在一般情况下,如果您希望在 Web 视图中运行的 JavaScript 与托管它的本机应用程序通信,您可以加载虚假 URL(例如:myapp://alert?The+text+of+the+警报+去+这里.").这将触发 webView:shouldStartLoadWithRequest:navigationType: 委托方法.在该方法中,检查请求,如果正在加载的 URL 是这些内部通信之一,则在您的应用中触发适当的操作,并返回 NO.

In the general case, if you want JavaScript running in a web view to communicate with the native app hosting it, you can load fake URLs (for example: "myapp://alert?The+text+of+the+alert+goes+here."). That will trigger the webView:shouldStartLoadWithRequest:navigationType: delegate method. In that method, inspect the request, and if the URL being loaded is one of these internal communications, trigger the appropriate action in your app, and return NO.

这篇关于我可以在 UIWebViewDelegate 中处理警报吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆