Reputation: 1403
I have 2 HTML5 App which use:
localStorage[key] = value;
to save data (I tried other localStorage methods like localStorage.keyName = "Bla" with same issue). The files are added as refernce files (blue folder) with this code to display on UIWebView:
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index"
ofType:@"html"
inDirectory:@"/some_path" ];
NSString *html = [NSString stringWithContentsOfFile:htmlPath
encoding:NSUTF8StringEncoding
error:nil];
[webView loadHTMLString:html
baseURL:[NSURL fileURLWithPath:
[NSString stringWithFormat:@"%@/some_path/",
[[NSBundle mainBundle] bundlePath]]]];
webView.scalesPageToFit = YES;
The weird thing is localStorage (javascript) works on one App and it doesn't on the other. On the one where it doesn't work I tried a try/catch code to get the error and I get a DOM 18 Security_Err. Sounds like it wouldn't allow me to access it because the files are local. It would make sense if this would be the case on both Apps.
I've no idea what's different since the code in both of them is almost the same. I've added no special feature for the UIWebView to disable any kind of special security restrictions.
I really don't know what's up. The only thing that's different is the HTML/JS/CSS side. I don't think this issue is being caused by the Cocoa Touch code since I literally copied the project to create the second App.
Thanks in advance and if you need more info please don't hesitate to ask. I wasn't able to find any solutions on Google or here. Info about localStorage key/value seems to be lacking. It makes sense since it's so easy to use but still. Anyone else having this issue?
Upvotes: 0
Views: 2938
Reputation: 11
Try this:
localStorage.removeItem('keyName');
localStorage.setItem('keyName','keyValue');
I believe you always have to remove before you set.
Upvotes: 1
Reputation: 1403
I found some kind of answer. I wonder if some other Cocoa Touch dev could shed some light on this. The code I use to load the html is this:
-(void)setUpWebview
{
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index"
ofType:@"html"
inDirectory:@"/proj_folder" ];
NSString *html = [NSString stringWithContentsOfFile:htmlPath
encoding:NSUTF8StringEncoding
error:nil];
[webView loadHTMLString:html
baseURL:[NSURL fileURLWithPath:
[NSString stringWithFormat:@"%@/proj_folder/",
[[NSBundle mainBundle] bundlePath]]]];
// disable scrolling on webview
[[[webView subviews] lastObject] setScrollEnabled:NO];
}
I get the "SECURITY ERR: DOM exception 18" on the first page I load (the index.html) only. Once I move onto other pages this doesn't happen. An awful workaround I found is reloading the index page when one lands there.
I'm sure there's a way of disabling this security feature from the Obj C side?
Thanks.
Upvotes: 0