Submit NSArray in javascript

In my application, I need to send an array from Objective-C to javascript. I read on the Internet that I can use this instruction: stringByEvaluatingJavaScriptFromString , also I did this:

Objective code snippet

 NSMutableArray *names = [[NSMutableArray alloc]init]; NSMutableArray *srcs = [[NSMutableArray alloc]init]; for (int i = 0; i < [site count]; i++) { NSDictionary *dictData = [site objectAtIndex:i]; [names addObject:[dictData objectForKey:@"name"]]; [srcs addObject:[dictData objectForKey:@"src"]]; } // UPDATE NSData *jsonArray = [self arrayToJson:nameSrc]; NSString *jsonString = [[NSString alloc]initWithData:jsonArray encoding:NSUTF8StringEncoding]; NSString *econdedString = [self base64String:jsonString]; NSString *jsCall = [NSString stringWithFormat:@"dataFromObjC(\"%@\")", econdedString]; [self.webView stringByEvaluatingJavaScriptFromString:jsCall]; 

So, in javascript, I created a function that has this name dataFromObjC(names, srcs) , but this does not show me the warning I made. I will write here the full code of my html so that you can help me solve this problem.

HTML code

 <!DOCTYPE html> <html lang="it"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/> <title>Lista coupon</title> <script src="../js/jquery-1.9.1.min.js" type="text/javascript"></script> <script src="../js/memoria.js" type="text/javascript"></script> <script type="text/javascript" src="../js/base64.js"></script> <script type="text/javascript"> function dataFromObjC(encodedArray) { var jsonString = Base64.decode(encodedArray); var arrayFromiOS = JSON.parse(jsonString); alert(jsonString); } </script> <style type="text/css"> body { background-color: #000000; width: 100%; height: 100%; padding: 0; margin: 0; } ul { list-style-type: none; padding: 5px; } li { color: #FFFFFF; font-family: sans-serif; padding-bottom: 5px; } p { color: #FFFFFF; font-family: sans-serif; padding: 5px; text-align: center; } a { text-decoration: none; color: #FFFFFF; } </style> </head> <body onload="loadJson();"> <div id="header"> </div> <div id="content"> <p>Di seguito trovi tutte le promozioni salvate</p> <div id="list"> </div> </div> <div id="footer"> </div> </body> </html> 

I hope you can help me.

thanks

+7
javascript objective-c uiwebview
source share
1 answer
  • First write your data in a JSON string instead of a simple string before sending it to JS. You can do this with NSJSONSerialization .

     - (NSData *) arrayToJSON:(NSArray *) inputArray { NSError *error = nil; id result = [NSJSONSerialization dataWithJSONObject:inputArray options:kNilOptions error:&error]; if (error != nil) return nil; return result; } 
  • Send it to JS using stringByEvaluatingJavaScriptFromString: If desired, I would recommend that you encode a Base64 string to avoid problems with special characters.

     // Convert your array to JSON data NSData *jsonArray = [self arrayToJSON: yourArray]; // Pass the JSON to an UTF8 string NSString *jsonString = [[NSString alloc] initWithData:jsonArray encoding:NSUTF8StringEncoding]; // Base64 encode the string to avoid problems NSString *encodedString = [Base64 encode:jsonString]; // Evaluate your JavaScript function with the encoded string as input NSString *jsCall = [NSString stringWithFormat:@"yourJsFunction(\"%@\")", encodedString]; [self.webView stringByEvaluatingJavaScriptFromString:jsCall]; 
  • Once in JS, parse the JSON string for the object (optional, remember to decode it if you are encoding first).

     function yourJsFunction(encodedArray) { // Decode and parse the received input var jsonString = Base64.decode(encodedArray); var arrayFromiOS = JSON.parse(jsonString); } 

UPDATE: About Base64 utilities for iOS and JS, there are many examples of functions and libraries on the Internet, such as those for iOS, and these other ones for JS. You can choose the one you prefer.

+9
source share

All Articles