4

I am trying to send an array to a javascript function via objective C.

I call the javascript function from my code by -

stringByEvaluatingJavaScriptFromString

I am now trying to pass an array of values to the javascript function.

This is what I tried -

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
     NSArray *array = [NSArray arrayWithObjects:@"10",@"9",@"8", nil];
     string = [[array valueForKey:@"description"] componentsJoinedByString:@","];        

    [graphView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"methodName2(%@)", string]];
}

In my Javascript -

        <script>
            function methodName(val)
            {
                alert(val);
            }
        </script>

However, only the number 10 gets displayed on my alert message on the webview. So I feel I am doing something wrong. Could someone tell me what I am doing wrong? And also, I would need to convert that string back into an array in the javascript.

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

learner2010
  • 4,157
  • 5
  • 43
  • 68

1 Answers1

6

try

[NSString stringWithFormat:@"methodName2([%@])", string]
georg
  • 211,518
  • 52
  • 313
  • 390
  • that worked!!! thank you very much.. would you be able to tell me why that worked though? – learner2010 Jan 17 '12 at 13:44
  • Also, I need to convert the string back into an array in javascript.. when I tried to use the 'split' function, it is not working.. is it because of the way I am passing the "String" to the javascript? – learner2010 Jan 17 '12 at 14:23
  • @learner2010, in the above code your variable "val" is already an array. No need to split it. – georg Jan 17 '12 at 16:16