我想为我的网络项目创建一个移动应用。我找到了phonegap。它说使用HTML,CSS和JavaScript轻松创建应用程序。我之前没有使用手机间隙创建移动应用程序。有三个存储选项memory-store.js(MemoryStore),ls-store.js(LocalStorageStore)和websql-store.js(WebSqlStore)。我只想保存一个令牌来识别用户。哪种存储最适合。有没有更好的方法来构建移动应用程序。
我感谢任何帮助。
答案 0 :(得分:42)
使用本地存储可能最容易满足您的需求。
从根本上讲,PhoneGap应用程序是本机应用程序(因此它们可以通过应用程序商店分发),只需运行一个或多个网页即可。然后,PhoneGap API将JavaScript钩子提供给设备功能,如相机等。它还有更多内容,但现在就是背景。
因为该应用程序本质上是一个网页(HTML5,CSS,JS),您可以使用LocalStorage(HTML5的一部分)。
本地存储空间使用示例:
设定值:
localStorage.myname = "Greg";
获取价值观:
localStorage.myname; // returns "Greg"
此处提供有关本地存储空间的更多信息:http://diveintohtml5.info/storage.html
对于Windows Phone 7:http://docs.phonegap.com/en/3.4.0/cordova_storage_storage.md.html#Storage
语法如下
localStorage.setItem("name", "Alen");
localStorage.getItem("name"); //will return Alen
答案 1 :(得分:1)
关于使用localStorage的一点是,它仅受HTML5兼容设备的支持。对于早期的设备(也是新设备的一个很好的选择),选项是使用phonegap的SQLite实现。 See here...
答案 2 :(得分:0)
我建议你也研究一下Lawnchair持久存储解决方案。它采用移动优先方法构建。我在一些项目中使用过它;它的效果非常好。
示例代码
var store = new lawnchair({name:'testing'}, function(store) {
// create an object
var me = {key:'brian'};
// save it
store.save(me);
// access it later... yes even after a page refresh!
store.get('brian', function(me) {
console.log(me);
});
});
了解更多相关信息
答案 3 :(得分:0)
朋友,我已经尝试过使用带有phonegap的cookie而没有成功。解决方案是使用localStorage。
关键快速示例:
var keyName = window.localStorage.key(0);
设置项目快速示例:
window.localStorage.setItem("key", "value");
获取物品快速示例
var value = window.localStorage.getItem("key");
// value is now equal to "value"
删除项目快速示例:
window.localStorage.removeItem("key");
清除快速示例:
window.localStorage.clear();
如果您在移动设备和网络上使用javascript,则可以使用此代码来检测环境:
var wl = window.location.href;
var mob = (wl.indexOf("android")>0);
参考文献: http://docs.phonegap.com/en/1.2.0/phonegap_storage_storage.md.html#localStorage http://cordova.apache.org/docs/en/6.x/cordova/storage/storage.html#page-toc-source
请注意:在iOS上使用匿名导航可能会使localstorage无法像spected那样工作。一个对我来说很好的简单测试:
$(document).ready(function () {
try {
localStorage.setItem('test', '1');
} catch (Err) {
if (Err.message.indexOf('QuotaExceededError') > -1) {
// Tell the user they are in anonymous mode
// Sugest it to go to https://support.apple.com/pt-br/HT203036 to get help to disable it
}
}
}
});