使用UIWebView调用Javascript [英] Calling Javascript using UIWebView

查看:101
本文介绍了使用UIWebView调用Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用函数在html页面中调用javascript -

I am trying to call a javascript in a html page using the function -

View did load function
{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"BasicGraph.html"];
    NSURL *urlStr = [NSURL fileURLWithPath:writablePath];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:@"BasicGraph" ofType:@"html"];
    [fileManager copyItemAtPath:myPathInfo toPath:writablePath error:NULL];

    [graphView loadRequest:[NSURLRequest requestWithURL:urlStr]];
}

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    [graphView stringByEvaluatingJavaScriptFromString:@"methodName()"];
}

这是html页面上的javascript -

Here is the javascript on the html page -

<script>
    function methodName()
      {
         // code to draw graph
      }

然而,函数 methodName()未被调用但在窗口之后被调用。 onload = function()一切正常..

However, the function methodName() is not getting called but after window.onload = function () everything is working fine..

我正在尝试集成 RGraphs 进入我的应用程序, Basic.html 是用于编写javascripts的html页面。

I am trying to integrate RGraphs into my application and Basic.html is the html page in which the javascripts are written.

It如果有人可以帮我解决这个问题会很棒。

It would be great if someone could help me out with this.

推荐答案

简单:你尝试从Objective-C执行JS函数在页面加载之前。

Simple: You try to execute the JS function from Objective-C before the page even has been loaded.

实施 UIWebView的委托方法 webViewDidFinishLoad:在您的UIViewController中并在那里调用 [graphView stringByEvaluatingJavaScriptFromString:@methodName()]; 确保在页面加载后调用函数。

Implement the UIWebView's delegate method webViewDidFinishLoad: in your UIViewController and in there you call [graphView stringByEvaluatingJavaScriptFromString:@"methodName()"]; to make sure the function gets called after the page has been loaded.

这篇关于使用UIWebView调用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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