20

I'm looking for a method to save data permanently using PhoneGap.

  1. LocalStorage
  2. File I/O
  3. ?

I heard LocalStorage is not 100% permanent, but is somewhere in the alley.

I wouldn't really like to begin with File I/O, because what I need is to create a database with recipes, and they need to stay.

"Number 3" is what I'm asking about. Any suggestions?

Thanks in advance :)

FranciscoBouza
  • 590
  • 6
  • 19
Daniel
  • 2,002
  • 5
  • 20
  • 32

4 Answers4

26

I'm using the localstorage and this is the best solution to keep saved data. Like you said, it isn't 100% permanent. In my case the localstorage is cleared only if you uninstall the app. Find more info about localstorage with Phonegap http://docs.phonegap.com/en/2.1.0/cordova_storage_storage.md.html#localStorage

You could get more info about localstorage on http://diveintohtml5.info/storage.html

You could use a database as well. You can find info on this at http://docs.phonegap.com/en/2.1.0/cordova_storage_storage.md.html#openDatabase

Thomas
  • 130
  • 4
A. Magalhães
  • 1,511
  • 2
  • 22
  • 31
  • 2
    @A.Magalhães doesn't the local storage data get removed if you change the default browser? I saw that on this post: http://stackoverflow.com/questions/7750857/how-permanent-is-local-storage-on-android-and-ios – Alex Stanese Apr 09 '14 at 22:05
  • 1
    It will be removed. See my answer below for instructions on how to save data that persists even if the app is re-installed. – Alex May 01 '15 at 20:34
6

If you would like to store the data on the device permanently even after the app in uninstalled, you will have to do it in a different way on iOS and android.

On iOS you can use the keychain https://github.com/shazron/KeychainPlugin.

On Android you can use the file plugin, and store the data on the SD card (cordova.file.externalRootDirectory).

In both cases, the data will remain on the device even if the user re-installs the app.

If you are using ionic (or just angular), you can use ng-persist that wraps these two plugins in an angular service with the same API for both ios and android.

More details here http://alexdisler.com/2014/04/23/persist-data-mobile-device-app-removed-uninstalled-cordova-ionic/

Alex
  • 1,712
  • 19
  • 16
0

You could do the following steps for saving any data permanently,

  1. Your HTML file

    <div id="deviceready">

    <div id="result"></div>
    

    </div>

  2. You JS file

    window.onload = function(){
    
        document.addEventListener("deviceready", init, false);
    }
    
    function init(){
     document.getElementById("signin").addEventListener("submit",login, false);
    }
    
    function saveData(){
      window.localStorage.setItem("header", 'Some Text Here');
    }
    
    function getData(){    
      document.getElementById("result").innerHTML = 
         window.localStorage.getItem("header");
      }
    }
    
Shahrukh Anwar
  • 2,544
  • 1
  • 24
  • 24
-1

In AppStore, when you release new version of your app, all of stored datas on your users will be reset. Before you update your app, you need to find better way to store your data.