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

46
  • Try logging in console in yourJsFunction function, to be sure you're going there at first place. Commented Oct 21, 2013 at 7:22
  • Also it's possible your arr parameter to be an ivalid json Commented Oct 21, 2013 at 7:24
  • divaka:i didnt get you? Commented Oct 21, 2013 at 7:26
  • divaka:when i print json string value i got this ["ഡാറ്റാ സെന്റര്‍ കേസ്: സത്യവാങ്മൂലം സമര്‍പ്പിക്കാന്‍ അനുമതി‌ ","കാശ്മീര്‍ നയത്തില്‍ മാറ്റമില്ലെന്ന് യു.എസ്‌","നിതാഖാത്‌ സമയപരിധി തീരാന്‍ 14 ദിവസം മാത്രം; ഇനി പിടിക്കപ്പെട്ടാല്‍ ജയില്‍ശിക്ഷയും പിഴയും‌ ","ഡീസല്‍ സബ്‌സിഡി: കെഎസ്ആര്‍ടിസി പുതിയ അപേക്ഷ സമര്‍പ്പിക്കണം‌ "] Commented Oct 21, 2013 at 7:26
  • 1
    Try changing option here NSJSONSerialization dataWithJSONObject: from kNilOptions to NSJSONWritingPrettyPrinted Commented Oct 21, 2013 at 12:24

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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];

Comments

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.