4

I have a requirement where the objective C code should consume the WebService and the response received in JSON format has to be passed as an argument to a javascript method for further processing.I am using UIWebView which will display the appropriate results after processing JSON response. The issue arises when i try to pass the JSON string to Javascript method.Javascript method does not simply accept the input.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSString *responseString = [[NSString alloc] initWithBytes:[resultData bytes] length:[resultData length] encoding:NSUTF8StringEncoding];
    NSLog(@"%@",responseString);
    [self returnResponseToJavaScriptMethods:responseString];
}


-(void)returnResponseToJavaScriptMethods:(NSString*)theResponse{
    [viewMainWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"retrieveServerResponse('%@')",theResponse]];
}

Is there anything that I am missing out ?? Simple strings are getting passed properly using the same approach.

UPDATE : Encoding the responseData to UTF8StringEncoding did the trick.

[theResponse stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]
Dalee Davis
  • 981
  • 8
  • 19
Mayank Sharma
  • 63
  • 1
  • 6

2 Answers2

3

You probably need to URL-escape the JSON string before posting it to the javascript.

Try to change:

-(void)returnResponseToJavaScriptMethods:(NSString*)theResponse{
  [viewMainWebView stringByEvaluatingJavaScriptFromString:
  [NSString stringWithFormat:@"retrieveServerResponse('%@')",theResponse]];
}

to

-(void)returnResponseToJavaScriptMethods:(NSString*)theResponse{
  [viewMainWebView stringByEvaluatingJavaScriptFromString:
  [NSString stringWithFormat:@"retrieveServerResponse('%@')",
 [theResponse stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
} 

And see if that helps you. Note, however, that there are better (or more surefire) ways to escape a string, for example the one given at this answer: How do I URL encode a string

Community
  • 1
  • 1
uvesten
  • 3,365
  • 2
  • 27
  • 40
  • Thanks a lot for your suggestion.It worked like a charm.But now the string which we padded with escape characters needs to be cleaned up to be converted as a proper JSON response.That is giving me another headache.Please do mention in comments if you know a way to perfectly reverse the encoding in javascript. – Mayank Sharma Jan 08 '13 at 09:00
  • I don't know if it's "perfect", but have you tried the `unescape()` function? It should work, just `unescape(your_escaped_json_string)` – uvesten Jan 08 '13 at 17:37
  • I changed my encoding approach.I just encode/decode it using Base64 encoding. – Mayank Sharma Jan 09 '13 at 05:59
0

There's no need for a retrieveServerResponse() in javascript, just use unescape(percentEscapedStringFromiOS) after you percent-escaped the string in iOS

NSString *encodedString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// CAVEAT: unescape(\"%@\") is important since "'" (single quote) isn't excaped by the above function!
NSString *jsString = [NSString stringWithFormat:@"myJSstring = unescape(\"%@\")", encodedString]];

NSString *jsResult = [self.webView stringByEvaluatingJavaScriptFromString:jsString];
AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
TiBooX
  • 677
  • 5
  • 5