23

I wanted to create an mobile app for my web project. I found phonegap. It says Easily create apps using HTML, CSS, and JavaScript. I have not created a mobile app using phone gap before. There are three storage options memory-store.js (MemoryStore), ls-store.js (LocalStorageStore) and websql-store.js (WebSqlStore). I just want to save a token to recognise the user. Which storage is best suited. Is there a better way to build an mobile app.

I appreciate any help.

4

4 回答 4

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
于 2013-04-12T10:51:55.670 回答
1

关于使用 localStorage 的一点要补充的是,它仅受 HTML5 兼容设备支持。对于较早的设备(对于新设备也是一个不错的选择),选项是使用 phonegap 的 SQLite 实现。看这里...

于 2014-05-23T07:30:18.520 回答
0

I would recommend you also look into Lawnchair persistent storage solution. It was built with a mobile first approach. I have used it in some projects; it works really well.

Sample code

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);
    });
});

You can read more about it at http://brian.io/lawnchair/

于 2013-04-13T14:17:10.793 回答
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-源

请注意:在 iOS 上使用匿名导航可能会使本地存储无法正常工作。一个对我来说很好的简单测试:

$(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
            }
        }
    }
});
于 2016-07-28T21:19:24.677 回答