Reputation: 1686
I am looking for send one NSArray from viewcontroller.m file to data.js file but its not printing anything in JavaScript. Following is my code.
In my viewdidload method
- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
// Black base color for background matches the native apps
CDV=[[CDVViewController alloc]init];
news=[[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"latest news" ofType:@"plist"]];
NSArray *newsItems=[[NSArray alloc]init];
newsItems=[news valueForKey:@"News"];
NSData *jsonArray = [self arrayToJSON:newsItems];
NSString *jsonString = [[NSString alloc] initWithData:jsonArray
encoding:NSUTF8StringEncoding];
NSString *jsCall = [NSString stringWithFormat:@"yourJsFunction([%@])", jsonString];
[theWebView stringByEvaluatingJavaScriptFromString:jsCall];
NSLog(@"%@",jsonString);
// theWebView.backgroundColor = [UIColor blackColor];
return [super webViewDidFinishLoad:theWebView];
}
- (NSData *) arrayToJSON:(NSArray *) inputArray
{
NSError *error = nil;
id result = [NSJSONSerialization dataWithJSONObject:inputArray
options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}
And in my one.js file I have this code:
function yourJsFunction(arr){
for(var i=0;i<arr.length;i++){
document.write(arr[i]);
}
return arr;
}
Output is:
ഡാറ്റാ സെന്റര് കേസ്: സത്യവാങ്മൂലം സമര്പ്പിക്കാന് അനുമതി,കാശ്മീര് നയത്തില് മാറ്റമില്ലെന്ന് യു.എസ്",നിതാഖാത് സമയപരിധി തീരാന് 14 ദിവസം മാത്രം; ഇനി പിടിക്കപ്പെട്ടാല് ജയില്ശിക്ഷയും പിഴയും,ഡീസല് സബ്സിഡി: കെഎസ്ആര്ടിസി പുതിയ അപേക്ഷ സമര്പ്പിക്കണം
But I want each of them separately instead of putting ","
Current output:
New image
Upvotes: 2
Views: 133
Reputation: 1867
Seems like you have passed a single string in newsItems
variable. Then you call yourJsFunction([%@])
- the argument is an array of your parameter (%@). Then you tried to print js object and got
["ഡാറ്റാ സെന്റര് കേസ്: സത്യവാങ്മൂലം സമര്പ്പിക്കാന് അനുമതി ","കാശ്മീര് നയത്തില് മാറ്റമില്ലെന്ന് യു.എസ്","നിതാഖാത് സമയപരിധി തീരാന് 14 ദിവസം മാത്രം; ഇനി പിടിക്കപ്പെട്ടാല് ജയില്ശിക്ഷയും പിഴയും ","ഡീസല് സബ്സിഡി: കെഎസ്ആര്ടിസി പുതിയ അപേക്ഷ സമര്പ്പിക്കണം "]
that is exactly array of one string object. Please check your file contents, i think it contain comma-separated values.
If it is then you have to break it into array of strings.
NSString *newsString = ...;// loading from plist
NSArray *newsItems = [newsString componentsSeparatedByString:","];
then serialize newsItems to json and call js function as
NSString *jsCall = [NSString stringWithFormat:@"yourJsFunction(%@)", jsonString];
instead of
NSString *jsCall = [NSString stringWithFormat:@"yourJsFunction([%@])", jsonString];
Upvotes: 0
Reputation: 1686
There are some changes in js file
1.Convert the arr to proper string using toString()
2.split that new string
var b=arr.toString().split(',');
now print document.write(b[1]); its working!!!!!
and in .m file Like "@Divaka" 's option
Try changing option here NSJSONSerialization dataWithJSONObject: from kNilOptions to NSJSONWritingPrettyPrinted
Upvotes: 1