Why You Can’t Trust Local Storage in Your Cordova App

Settings are an important part of any mobile app. A mobile app is nothing without user data, which makes it especially important to ensure settings are never lost. Now, if you are a Javascript developer of any sorts, your first thoughts would be to store these user settings directly in localStorage. In fact, that is what most tutorials on the web actually tell you to do. However, I am here to tell you differently. If you intend on making a hybrid mobile application with Cordova, DO NOT use localStorage. Here is why:

I run a mobile app with a few thousand users, and every once in awhile I will receive an email from a user saying that their settings were randomly getting wiped. At first, I mainly just shrugged it off as user’s just doing “weird things,” however the more my app grew, the more this edge case came up. I got a few bad reviews in the app store, which killed my 5-star rating, so I decided to actually look into it.

The app I run is on Android and iOS, however, after doing a little research, I found that every user who had reported the problem was on iOS. There were no reports of this mysterious issue on Android. At first, my thought was that the user must have wiped their browser history and that was what was triggering localStorage to clear inside my app. However, after trying to replicate this, I quickly realized this was not the case. No matter how many times I cleared my browser history on my iPhone, localStorage inside my app stayed put. I tried a bunch of different things to attempt to replicate this problem (one user thought it even had to do with Snapchat), and no matter what I did, I was unsuccessful in replicating the problem.

After doing some research on the web, a few other developers had reported this issue as well. What seems to happen is as soon as iOS is low on memory, the OS starts randomly wiping data. There’s no predicting what data will get wiped or when, but it seems as if iOS has no problem wiping data inside localStorage when running low on memory.

So what can you do to make sure you never run into this issue? Store the settings natively. If you are using Ionic, there are plugins that will handle everything for you. It works across Android and iOS, and it is very simple to use. Moral of the story: DO NOT trust tutorials out there that tell you to store data inside localStorage. You will get some very upset users down the line!