您的位置:首页 > Web前端 > HTML5

native 嵌套 h5 本地存储问题

2016-05-11 17:20 651 查看
native 嵌套h5 本地存储问题,按照正常逻辑来说(localStorage、sessionStorage),本是没有任何问题的。

但是 native 嵌套之后,问题就出现了,就是localStorage/sessionStorage 存储值的时候出问题了,都会在native 端报 null,无法使用本地存储,难道是这样吗?难道不支持吗?顺间,你会感觉到一大堆的问题都在h5上。

其实都是native 权限的事儿,IOS如何设置权限就不知道了,

Android webview 如何打开本地存储,提供给JS调用 html5 的lwindow.localStorage功能

mWebView = (WebView) this.findViewById(R.id.webview);

WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
//settings.setPluginsEnabled(true);

/***打开本地缓存提供JS调用**/
mWebView.getSettings().setDomStorageEnabled(true);
// Set cache size to 8 mb by default. should be more than enough
mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);

// This next one is crazy. It's the DEFAULT location for your app's cache
// But it didn't work for me without this line.
// UPDATE: no hardcoded path. Thanks to Kevin Hawkins
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
mWebView.getSettings().setAppCachePath(appCachePath);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAppCacheEnabled(true);


PS:Java的navtive代码只要设置了以上参数,就可以为JS端提供本地存储了,但是这个参数需要API>=7使用,也就是android2.1版本以上才可以。

详情查阅:这里写链接内容
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: