2

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:

enter image description here

New image enter image description here

Dmitry Shvedov
  • 3,169
  • 4
  • 39
  • 51
Navi
  • 1,696
  • 2
  • 17
  • 36
  • Try logging in console in yourJsFunction function, to be sure you're going there at first place. – divaka Oct 21 '13 at 07:22
  • Also it's possible your arr parameter to be an ivalid json – divaka Oct 21 '13 at 07:24
  • divaka:i didnt get you? – Navi Oct 21 '13 at 07:26
  • divaka:when i print json string value i got this ["ഡാറ്റാ സെന്റര്‍ കേസ്: സത്യവാങ്മൂലം സമര്‍പ്പിക്കാന്‍ അനുമതി‌ ","കാശ്മീര്‍ നയത്തില്‍ മാറ്റമില്ലെന്ന് യു.എസ്‌","നിതാഖാത്‌ സമയപരിധി തീരാന്‍ 14 ദിവസം മാത്രം; ഇനി പിടിക്കപ്പെട്ടാല്‍ ജയില്‍ശിക്ഷയും പിഴയും‌ ","ഡീസല്‍ സബ്‌സിഡി: കെഎസ്ആര്‍ടിസി പുതിയ അപേക്ഷ സമര്‍പ്പിക്കണം‌ "] – Navi Oct 21 '13 at 07:26
  • It seems valid. What about encoding? Is it UTF-8? You could also try printing arrayFromiOS. – divaka Oct 21 '13 at 07:30
  • no am not encoding it – Navi Oct 21 '13 at 07:35
  • You are not encoding it, but when you create the .plist file it has some encoding. Try printing arrayFromiOS after you parsing the arr parameter. – divaka Oct 21 '13 at 07:38
  • ok let me try and i will let u knwpo – Navi Oct 21 '13 at 07:40
  • Divaka:this line is not printing anything for(var i=0;i – Navi Oct 21 '13 at 07:42
  • In `yourJsFuncion` print `arr` parameter before `var arrayFromiOS = JSON.parse(arr);` line and after this line print `arrayFromiOS` – divaka Oct 21 '13 at 07:44
  • its not printing anything...how do i know whether its executing or not??? – Navi Oct 21 '13 at 07:48
  • Check your javascript console on your browser? – divaka Oct 21 '13 at 07:49
  • but am running all these from xcode ryt – Navi Oct 21 '13 at 07:51
  • Check this: http://stackoverflow.com/questions/6656544/stringbyevaluatingjavascriptfromstring-does-not-work – divaka Oct 21 '13 at 07:55
  • You need `(void)webViewDidFinishLoad:(UIWebView *)webView` method, and there you should put `[self.webView stringByEvaluatingJavaScriptFromString:jsCall];` – divaka Oct 21 '13 at 07:57
  • have added that one still not working – Navi Oct 21 '13 at 07:58
  • Take a look at this http://stackoverflow.com/questions/13042480/uiwebview-stringbyevaluatingjavascriptfromstring-isnt-calling-javascript-functi – divaka Oct 21 '13 at 08:02
  • i think my problem is index.html not linking that js file – Navi Oct 21 '13 at 08:03
  • How are you loading it? – divaka Oct 21 '13 at 08:07
  • Take a look at this. It could help you: http://stackoverflow.com/questions/5733883/loading-javascript-into-a-uiwebview-from-resources/13473498 – divaka Oct 21 '13 at 08:15
  • Why main1.js? The function is in data.js? Check the links I gave you! – divaka Oct 21 '13 at 08:22
  • i just rename that data.js to main1.js – Navi Oct 21 '13 at 08:22
  • Check the links I gave you. How did you add data.js to your resources? – divaka Oct 21 '13 at 08:23
  • now the html links with js file ,but its still not printing anything – Navi Oct 21 '13 at 08:45
  • Maybe it's not properly linked. did you check the links I gave you? – divaka Oct 21 '13 at 12:07
  • If you want them separate use js `split` method. – divaka Oct 21 '13 at 12:09
  • divaka:yes ..i hav checked those links..now am able to get data . – Navi Oct 21 '13 at 12:11
  • So now what is the problem? You want to separate a string by ','? – divaka Oct 21 '13 at 12:12
  • Please check the image in my question.. it showing all four elements with "," but i want to display only one of them – Navi Oct 21 '13 at 12:13
  • i have tried split in js but its not working – Navi Oct 21 '13 at 12:14
  • One on each row? Where did you apply .split method? I'm pretty sure your `arr` in `yourJsFunction` consists of 1 element – divaka Oct 21 '13 at 12:18
  • i have tried some thing like var value=arr; var i=value.split(","); document.write(i); but its printing anything – Navi Oct 21 '13 at 12:19
  • Could you print `arr.length` in `yourJsMethod` – divaka Oct 21 '13 at 12:21
  • yup i print that one document.write(arr.length); result is 1 – Navi Oct 21 '13 at 12:22
  • 1
    Try changing option here `NSJSONSerialization dataWithJSONObject:` from `kNilOptions` to `NSJSONWritingPrettyPrinted` – divaka Oct 21 '13 at 12:24
  • If this does not work in `yourJsFunction` at the top add `arr = arr[0].split(',');` – divaka Oct 21 '13 at 12:25
  • the split option is not even showing ,please check the new image in my question.and i have made change in NSJSONWritingPrettyPrinted but it still showing length =1 – Navi Oct 21 '13 at 12:27
  • Add `arr = arr[0].split(',');` at the beginning of `yourJsfunction`; – divaka Oct 21 '13 at 12:28
  • u mean arr = arr.split(',')???? – Navi Oct 21 '13 at 12:29
  • it's obvious that you arr contains 1 element, which is "aaaa,bbbb,cccc,dddd". So you get the first element with `arr[0]`, make array out of it by splitting on ',' and the rest code stays the same – divaka Oct 21 '13 at 12:31
  • its not showing "split" option it showing only "switch and "super" – Navi Oct 21 '13 at 12:34
  • `arr = arr[0].split` there is an index – divaka Oct 21 '13 at 12:35
  • i got the solution ,now its working ..first we have to convert that to a proper string using toString () then split that one. var b=arr.toString().split(','); now its working..thanks alot – Navi Oct 21 '13 at 12:37
  • divaka :wer r u from? – Navi Oct 21 '13 at 12:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39649/discussion-between-divaka-and-navi) – divaka Oct 21 '13 at 12:41

2 Answers2

1

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

Navi
  • 1,696
  • 2
  • 17
  • 36
0

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];
storoj
  • 1,851
  • 2
  • 18
  • 25