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.

Leah Collins
  • 637
  • 2
  • 9
  • 21

4 Answers4

42

Using Local Storage will probably be easiest for your needs.

Fundamentally speaking PhoneGap apps are native apps (so they can be distributed through app stores) that simply run a web page or pages. The PhoneGap API then provides JavaScript hooks into the device functions like camera etc. Theres more to it but for now thats the background.

So since the app is essentially a web page (HTML5, CSS, JS) you can make use of LocalStorage (part of HTML5).

Example local storage usage:

Setting values:

localStorage.myname = "Greg";

Getting values:

localStorage.myname; // returns "Greg"

More information here for local storage: http://diveintohtml5.info/storage.html

For Windows Phone 7 : http://docs.phonegap.com/en/3.4.0/cordova_storage_storage.md.html#Storage

The syntax is as below

localStorage.setItem("name", "Alen");

localStorage.getItem("name"); //will return Alen
Alen Saqe
  • 479
  • 7
  • 25
Greg
  • 31,180
  • 18
  • 65
  • 85
  • does localstorage store values if the app is closed and reopened. I appreciate your help. – Leah Collins Apr 12 '13 at 12:05
  • 2
    Yep. Even when the phone is turned off. Remember though that you only have a 5mb limit with LocalStorage. More on that link at the bottom... – Greg Apr 12 '13 at 12:21
  • 1
    Ooh Great solution Dude... Cheerzzz – chhameed Jul 26 '13 at 07:16
  • I have a question. Does Local Storage work on Windows Phone devices? – Renato Ramos Nascimento Jun 02 '14 at 16:35
  • I does on my Windows Phone 8 app. So it includes 8.1 but im quite sure it works even on WP 7 the only problem is that you can use this syntax localStorage.myname = "Greg"; you have to use another one like. localStorage.setItem ("name", "Greg"); and localStorage.getItem("name") // Will be Greg :) – Alen Saqe Jun 04 '14 at 16:31
  • 1
    @RenatoRamosNascimento i edited Gregs Answer to reflect your needs! – Alen Saqe Jun 04 '14 at 16:35
1

A point to add about using localStorage is that it's only supported by HTML5-compliant devices. For earlier devices (also a fine choice for new devices) the option is to use phonegap's SQLite implementation. See here...

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
Hammer
  • 8,538
  • 12
  • 44
  • 75
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/

FranciscoBouza
  • 590
  • 6
  • 19
apushpar
  • 37
  • 4
0

Friend, I've tried too without success to use cookies with phonegap. The solution was use localStorage.

Key Quick Example:

 var keyName = window.localStorage.key(0);

Set Item Quick Example:

 window.localStorage.setItem("key", "value");

Get Item Quick Example

 var value = window.localStorage.getItem("key");
 // value is now equal to "value"

Remove Item Quick Example:

 window.localStorage.removeItem("key");

Clear Quick Example:

 window.localStorage.clear();

If you use you javascript for both mobile and web, you can use this code to detect that enviroment:

var wl = window.location.href;
var mob = (wl.indexOf("android")>0);

References: 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

Be aware: using anonymous navigation on iOS may make localstorage not work like spected. A simple test that are working fine to me:

$(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
            }
        }
    }
});
Tiago Gouvêa
  • 15,036
  • 4
  • 75
  • 81