1

I have a webview from which I'm calling a method that returns result of JSON.stringify(); Then it is stored in database and used later to restore state of the page in webview.

NSString *data = [self.browser stringByEvaluatingJavaScriptFromString:@"course.serialize();"];
[self.course storeRawSerializedData:data];

when it is retrieved from database (Core Data) I'm injecting it into loaded html page source like so:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    if(_data){
        NSString* content = [[NSString alloc] initWithData:_data
                                                  encoding:NSUTF8StringEncoding];
        _data = nil;
        _jsReady = YES;

        NSString *data = @"";
        if(self.course.returnRawSerializedData){
            data = [data stringByAppendingString:@"<script type=\"text/javascript\">var restoreData=%@;</script>"];
            data = [NSString stringWithFormat:data, self.course.returnRawSerializedData];
        }
        //inject extra content to the page
        content = [data stringByAppendingString:content];
        //load modified HTML string
        [self.browser loadHTMLString:content baseURL:_url];
    }
}

The problem is with JSON being invalid after this trip:

the json returned by webview is:

{"status":"incomplete","data_json":"{\"colour\":\"#000000\"}"}

and after injecting it looks like this:

<script type=\"text/javascript\">var restoreData={"status":"incomplete","data_json":"{\"colour\":\"#000000\"}"};</script>

The page returns the SyntaxError: JSON parse error: Unexpected identifier "object"

When I wrap it with single or double quotes in stringByAppendingString method then page returns the SyntaxError: JSON parse error: Expected '}'

It works when I escape all double quotes (manually) but I don;t know how to do it programatically, I expected that string returned from stringByEvaluatingJavaScriptFromString will be already escaped to be used within double quotes.

Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79
  • Possible duplicate of http://stackoverflow.com/questions/14198331/how-to-pass-json-nsstring-containing-json-to-a-javascript-method-from-an-objecti – 2intor Apr 14 '14 at 11:06
  • It is not and proposed solution within the given SO question doesnt fix my problem (and how could it when it escapes to percent encoded and I need escaped quotes!). – Lukasz 'Severiaan' Grela Apr 14 '14 at 11:30
  • I use the solution proposed [here](http://stackoverflow.com/a/770533/923340) which is not ideal as it means that my serialize method will do escaping. However it would be better if I could have a objective-c based solution. – Lukasz 'Severiaan' Grela Apr 14 '14 at 12:15

2 Answers2

1

you can also consider following as solution,

you Base64 encode NSString that you want to inject in Objective-C do the reverse in Javascript.

2intor
  • 1,044
  • 1
  • 8
  • 19
  • This is variation of the solution that I've used now (see my comment with link to source) and it is not ideal solution, this time I have control over the JS and I can modify returned value or in this case injected value but it may not be always possible. I expected that there is built in solution or at least some methods that will do it for me. – Lukasz 'Severiaan' Grela Apr 15 '14 at 06:31
  • I'm accepting this as anyway this is how I'm doing this now, but shame that there is no built in solution. – Lukasz 'Severiaan' Grela Jun 10 '14 at 21:11
0

Escape quotes will work.

For example:

content = [content stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
Pang
  • 9,564
  • 146
  • 81
  • 122
Ankit
  • 141
  • 1
  • 3