36

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

Here is the javascript on the html page -

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

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

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.

learner2010
  • 4,157
  • 5
  • 43
  • 68
  • Hey, if you don't mind can you please share your sample code where i can call rgrpahs from native iOS code? It will be very helpful. from last one week i am struggling to make it work. – iamMobile Jul 27 '12 at 11:12
  • 1
    I just created an elegant demo on the topic of Javascript run in UIWebview, please refer to :http://creiapp.blogspot.hk/2013/04/uiwebview-javascript.html – Cullen SUN Apr 18 '13 at 05:43

2 Answers2

68

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

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.

Björn Kaiser
  • 9,882
  • 4
  • 37
  • 57
  • I tried to do that but still it is not working.. I have changed the code in the question but nothing seems to work.. – learner2010 Jan 16 '12 at 22:02
  • 7
    it is a very valid question and not at all a stupid one.. thank you. i forgot to set the delegate.. and the code works now... – learner2010 Jan 16 '12 at 22:06
10

To clarify a little bit more.

.h - implement the UIWebViewDelegate

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

.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.

Kalel Wade
  • 7,742
  • 3
  • 39
  • 55