iOS7 unable to access HTML5 localStorage, if Cookies is disabled

ⅰ亾dé卋堺 提交于 2019-12-22 02:03:37

问题


On iOS7, while trying to access HTML5 localStorage object via JavaScript, stops execution of Javascript. It works correctly in previous iOS versions. The app runs perfectly when the app is freshly installed and launched. But then, I close the app and launch again, it does not work and fails on accessing localStorage.

To disable cookies, I have used below code:

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];

To access localStorage, I have used below code:

CGRect rect = self.view.frame;
UIWebView* webView = [[UIWebView alloc] initWithFrame:rect];
[self.view addSubview:webView];
[webView loadHTMLString:@"<script>alert('hello world');alert(localStorage);</script>" baseURL:nil]

I have gone through the iOS7 docs but could not find any update related to this issue. Has anyone faced this too? Please share any information regarding this, would be very helpful. Any workarounds for this?

Thanks


回答1:


Granted this is an older post, but fwiw ...

Not sure if I have experienced this issue. Have used localStorage with and without cookies on several projects using iOS6/7.

The loadHTMLString provided looks a little peculiar to me ... so I tried this instead...

    [webView stringByEvaluatingJavaScriptFromString:@"alert('hello world');alert(localStorage);"];

... which worked fine, and showed the localStorage object as expected, with cookies disabled.

Tested in sim and on device, iOS7.




回答2:


Not sure if this will help but you can go through a blog here-- http://transitionalreality.blogspot.in/2014/03/html-5-local-storage-with-ios-app.html

I have implemented HTML5 local storage in my own way and this might be what you are looking for.




回答3:


Try:

window.localStorage

e.g:

[webView loadHTMLString:@"<script>alert('hello world');alert(window.localStorage);</script>" baseURL:nil];


来源:https://stackoverflow.com/questions/18932147/ios7-unable-to-access-html5-localstorage-if-cookies-is-disabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!