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.

1
  • It would be great if this could be accomplished by passing an object, not just a string. Oh well. Commented Feb 14, 2012 at 14:26

1 Answer 1

6

try

[NSString stringWithFormat:@"methodName2([%@])", string]
Sign up to request clarification or add additional context in comments.

3 Comments

that worked!!! thank you very much.. would you be able to tell me why that worked though?
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, in the above code your variable "val" is already an array. No need to split it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.