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]

2 Answers 2

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

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

3 Comments

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.
I don't know if it's "perfect", but have you tried the unescape() function? It should work, just unescape(your_escaped_json_string)
I changed my encoding approach.I just encode/decode it using Base64 encoding.
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];

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.