简体   繁体   English

使用UIWebView调用Javascript

[英]Calling Javascript using UIWebView

I am trying to call a javascript in a html page using the function - 我正在尝试使用函数在html页面中调用javascript-

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()"];
}

Here is the javascript on the html page - 这是html页面上的javascript-

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

However, the function methodName() is not getting called but after window.onload = function () everything is working fine.. 但是,函数methodName()不会被调用,而是在window.onload = function()之后一切正常。

I am trying to integrate RGraphs into my application and Basic.html is the html page in which the javascripts are written. 我正在尝试将RGraphs集成到我的应用程序中,而Basic.html是其中编写JavaScript的html页面。

It would be great if someone could help me out with this. 如果有人可以帮我解决这个问题,那就太好了。

Simple: You try to execute the JS function from Objective-C before the page even has been loaded. 简单:您甚至在页面加载之前尝试从Objective-C执行JS函数。

Implement the UIWebView's delegate method webViewDidFinishLoad: in your UIViewController and in there you call [graphView stringByEvaluatingJavaScriptFromString:@"methodName()"]; 在UIViewController中实现UIWebView的委托方法 webViewDidFinishLoad:然后在其中调用[graphView stringByEvaluatingJavaScriptFromString:@"methodName()"]; to make sure the function gets called after the page has been loaded. 确保在加载页面调用该函数。

To clarify a little bit more. 要澄清一点。

.h - implement the UIWebViewDelegate .h-实现UIWebViewDelegate

@interface YourViewController : UIViewController <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end

.m .m

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *path = @"http://www.google.com";
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]]];
    _webView.delegate = self; //Set the webviews delegate to this
}

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    //Execute javascript method or pure javascript if needed
    [_webView stringByEvaluatingJavaScriptFromString:@"methodName();"];
}

You could also assign the delegate from storyboard instead of doing it in the code. 您也可以从情节提要中分配委托,而不用在代码中进行委托。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM