learner2010
learner2010

Reputation: 4167

Passing an array to a javascript function from objc

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.

Upvotes: 4

Views: 3516

Answers (1)

georg
georg

Reputation: 214959

try

[NSString stringWithFormat:@"methodName2([%@])", string]

Upvotes: 6

Related Questions