Skip to main content

Use Ionic Storage To Save Data In Ionic App

In this post, we will take a look at how you can save data in Ionic apps using Ionic Storage. The data that we will store will be persistent, meaning it will remain in memory even if the app is closed or restarted.

Storing data in ionic using Ionic Storage is a great way to store data that is important to the working of your app such as an auth token or some other activity that you need to track. Let me make one thing clear you should NEVER save passwords locally. Although data saved in one app cannot be accessed by another app but saving passwords locally is a big no-no.

The way that Ionic Storage works is that it chooses the storage technologies such as SQLite, IndexedDB, WebSQL, and localStorage automatically depending on availability and preference. While running on a device it will give priority to SQLite over the other. In the case of a Progressive Web App, it will give priority to IndexedDB, WebSQL, and localStorage in order.

We will create a simple app that will have two buttons, one will save our data and the other will get the data. The data that we will save will be a JSON object, however, you can also store a string.

Step 1)

Let’s create a new Ionic app. We do that by running the following command.

Note here that I am using the blank template, also note that I have navigated my terminal inside the newly created project.

 

Step 2)

Now we need to add the cordova-sqlite-storage plugin. Only add this plugin if you wish to run your app on a Native iOS or Android device. The plugin is not necessary for a PWA as explained above. To add the plugin we will run the following code.

Here we have also added the Ionic-Native wrapper for the plugin as well.

Step 3)

We will need to add IonicStorageModule to our app’s module, so open up app\app.module.ts and code it as shown below.

 

Step 4)

Now it’s time to code up our buttons. Open up \src\pages\home\home.html and code it as follows.

Here we are creating two buttons, one to set the value and another to get the value.

Step 5)

Now let’s code the HomePage component, open up \src\pages\home\home.ts and code it as follows.

Here first we are importing Storage and injecting it into our constructor. Then inside of the constructor, we are initializing it with some data. Inside the setValue() function we are saving the data to memory by doing this.storage.set(). The .set() method accepts two arguments the first is a key, the second argument is an object to be stored. The .set() function returns a promise, in the success callback of the promise we get the object that was saved returned to us. This is great if you want to perform some operation on data once it is saved.

We can get the stored the data by using the this.storage.get() and passing the key which we used to save the data.

Conclusion

In this example, we are logging the data in this example just to illustrate the working of storage you can store any data you want. Make sure that you are not storing any sensitive information like passwords using it.

Download Code:

Code Will Be Sent Via Email

 

Leave Me Your Questions And Feedback

Getting your feedback inspires me the most so hit me up in the comments here or on the comments section on my YouTube Channel or on twitter @pointDeveloper
Consider Subscribing / Donating
Subscribe To Mailing List
Donate
If my site was helpful to you, then please consider donating via paypal:

Prantik Vaghela (pointdeveloper)

Hi my name is Prantik Vaghela. I have done my Bachelors and Masters in Computer Science and am a web developer and now a Tutor through my blog. :-)