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?
-
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?F.X.– F.X.2013-04-23 09:58:50 +00:00Commented Apr 23, 2013 at 9:58
-
ya i am loading the webview on button's click and calling the javascript using stringEvaluatingJavaScriptFromStringuser2284897– user22848972013-04-23 10:09:27 +00:00Commented Apr 23, 2013 at 10:09
-
my code on button's click is:user2284897– user22848972013-04-23 10:09:47 +00:00Commented Apr 23, 2013 at 10:09
Add a comment
|
2 Answers
Pass an array as a parameter from objective c to Javasctipt
NSString *arrayStr = [currentArray componentsJoinedByString:@"','"];
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"initActivity(['%@'])", arrayStr ]];
1 Comment
Josh Kethepalli
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);
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
user2284897
then how will i recieve the array in javascript??
user2284897
bt here the value will be passes as variables....... i want to directly pass the array and access that array in javascript
user2284897
@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
Markus
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.user2284897
can u plz elleborate on this.how will i pass it and how will i handle it.
|