使用 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()
没有被调用,但是在 window.onload = function () 之后一切正常..
However, the function methodName()
is not getting called but after window.onload = function () everything is working fine..
我正在尝试将 RGraphs 集成到我的应用程序中,而 Basic.html
是其中的 html 页面javascript是写的.
I am trying to integrate RGraphs into my application and Basic.html
is the html page in which the javascripts are written.
如果有人能帮我解决这个问题,那就太好了.
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.
相关文章