与嵌入式UIWebView之间来回传递数据 [英] Passing Data To and From an Embedded UIWebView

查看:78
本文介绍了与嵌入式UIWebView之间来回传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图控制器中嵌入了一个UIWebView,如下所示:

I have a UIWebView embedded in my view controller like this:

我在网络视图(_graphTotal)上有一个出口,可以使用以下方法成功在其中加载test.html的内容:

I have an outlet to my web view (_graphTotal) and I can successfully load the contents of test.html in it using this:

[_graphTotal loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"]isDirectory:NO]]];

[_graphTotal loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"]isDirectory:NO]]];

我现在正尝试将数据传递到Web视图,但没有任何运气.我添加了<UIWebViewDelegate>,这是我要尝试的:

I'm now trying to pass data to the web view and haven't had any luck. I have added the <UIWebViewDelegate> and here's what I'm trying:

NSString *userName = [_graphTotal stringByEvaluatingJavaScriptFromString:@"testFunction()"];
NSLog(@"Web response: %@",userName);

这是我项目中test.html的内容:

<html>
  <head></head>
  <body>
    Testing...
      <script type="text/javascript">
        function testFunction() {
          alert("made it!");
          return "hi!";
        }
        </script>
  </body>
</html>

我可以在webView中看到正在测试...",但是我既没有看到警报,也没有看到返回的嗨!".字符串.

I can see "Testing..." in my webView, but I'm not seeing the alert nor the returned "hi!" string.

知道我在做什么错吗?

推荐答案

好吧,问题是您试图在Webview有机会加载页面之前评估javascript.首先在您的视图控制器中采用<UIWebViewDelegate>协议:

Well the problem is that you tried to evaluate your javascript before the webview had the chance to load the page. First adopt the <UIWebViewDelegate> protocol into your view controller:

@interface ViewController : UIViewController <UIWebViewDelegate>

然后将您的Webview的代表连接到您的视图控制器,发出请求并最终实现

Then connect the delegate of your webview to your view controller, make the request and finally implement the delegate methods. Here is the one that notifies you when the webview has finished loading:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.graphTotal loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"]isDirectory:NO]]];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *userName = [self.graphTotal stringByEvaluatingJavaScriptFromString:@"testFunction()"];
    NSLog(@"Web response: %@",userName);
}

PS::以这种方式评估javascript时,必须了解的限制之一是必须在10秒内执行脚本.因此,例如,如果您等待了10秒钟以上以关闭警报,则会收到错误消息(等待10秒钟后无法返回).在 中找到有关限制的更多信息.文档 .

PS: One of the limits you must be aware of when you're evaluating javascript this way is that your script must be executed within 10 seconds. So if for example you've waited more than 10 secs to dismiss the alert you would get an error (failed to return after waiting 10 seconds). Find more about the limitations in the docs.

这篇关于与嵌入式UIWebView之间来回传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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