0

I want to pass the NSArray from Objective C on a button's click to JavaScript. How do I pass it? I'm able to call the JavaScript method with parameters but how do I achieve this?

3
  • Pretty hard to give an answer if we don't know how you're using Obj-C and JS together. UIWebView with some custom code? Running your own JS engine? Commented Apr 23, 2013 at 9:58
  • ya i am loading the webview on button's click and calling the javascript using stringEvaluatingJavaScriptFromString Commented Apr 23, 2013 at 10:09
  • my code on button's click is: Commented Apr 23, 2013 at 10:09

2 Answers 2

1

Pass an array as a parameter from objective c to Javasctipt

NSString *arrayStr = [currentArray componentsJoinedByString:@"','"];
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"initActivity(['%@'])", arrayStr ]];
Sign up to request clarification or add additional context in comments.

1 Comment

extension for the above answer: Here you are able to send a string in arrayformat. In Java Script you are able to retrieve String. If you want to convert as an Array Follow the steps: // converting json string to Array format var jsonEle = JSON.parse(arrayOfJSONElements);
0

If your array contains primitive values or strings you can do the following:

NSArray *objs; 
NSString *arrayStr = [objs componentsJoinedByString:@","];
NSString *jsFunc = [NSString stringWithFormat:@"jsFuncName([%@])", arrayStr];
[webViev stringByEvaluatingJavaScriptFromString:jsFunc];

If your array contains some other type, you need to stringify each object before calling componentsJoinedByString.

12 Comments

then how will i recieve the array in javascript??
bt here the value will be passes as variables....... i want to directly pass the array and access that array in javascript
@FX here the array will be passed as string for ex-jsFuncNAme(02,ertjkh,krtj) and i woll have to handle each element of array individually as a variable ...whereas i want to access it as an array in javascript
Assume: NSArray *obj = @[@"one", @"two", @"three"] then you can have a js function jsFuncName(inputArray)where inputArray will have 3 elements when invoked with obj.
can u plz elleborate on this.how will i pass it and how will i handle 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.