javascript - NSString from webview with JSON string -
i have webview i'm calling method returns result of json.stringify(); stored in database , used later restore state of page in webview.
nsstring *data = [self.browser stringbyevaluatingjavascriptfromstring:@"course.serialize();"]; [self.course storerawserializeddata:data]; when retrieved database (core data) i'm injecting loaded html page source 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 content page content = [data stringbyappendingstring:content]; //load modified html string [self.browser loadhtmlstring:content baseurl:_url]; } } the problem json being invalid after trip:
the json returned webview is:
{"status":"incomplete","data_json":"{\"colour\":\"#000000\"}"}
and after injecting looks this:
<script type=\"text/javascript\">var restoredata={"status":"incomplete","data_json":"{\"colour\":\"#000000\"}"};</script>
the page returns syntaxerror: json parse error: unexpected identifier "object"
when wrap single or double quotes in stringbyappendingstring method page returns syntaxerror: json parse error: expected '}'
it works when escape double quotes (manually) don;t know how programatically, expected string returned stringbyevaluatingjavascriptfromstring escaped used within double quotes.
you can consider following solution,
you base64 encode nsstring want inject in objective-c reverse in javascript.
Comments
Post a Comment