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.

3
  • Possible duplicate of stackoverflow.com/questions/14198331/… Commented Apr 14, 2014 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!). Commented Apr 14, 2014 at 11:30
  • I use the solution proposed here 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. Commented Apr 14, 2014 at 12:15

2 Answers 2

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.

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

2 Comments

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.
I'm accepting this as anyway this is how I'm doing this now, but shame that there is no built in solution.
0

Escape quotes will work.

For example:

content = [content stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];

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.