iOS localstorage persistence

How do I get local storage to persist on iOS with ionic? It gets wiped out randomly every few hours and even minutes

There’s nothing special to do - it should just work without hassle. Do make sure that you are using localStorage.setItem() / localStorage.getItem() and not the localStorage[“xxx”] style syntax. I’ve noticed on some browsers the [ ] syntax can behave like you described.

I’m utilizing the localstorage factory that max did. I’m setting the object using $localstorage.setObject(‘user’,{id:id,expires:time}) and retrieving it within a resolve on my controller with $localstorage.getObject(‘user’). It redirects the user to the Sign in page if the id value is empty. Though it only persists for a short while before I reopen the app and I’m directed to the signin page

I’m not familiar with the localstorage factory method but maybe try the ‘old-fashioned’ way and see if it works differently.

check out . You can configure cache to be sessionStorage or localStorage. localStorage will not be wiped out unless the app is uninstalled or upgraded.

Have a look at LocalForage

1 Like

Do you thing its better to use LocalStorage than SQLite ?

I have not only tried the $localStorage factory but I have tried angular-cache as well as localForage and to no avail. They work for a few minutes though once I close the app and go into another app for more than a few minutes then try to reopen the app, the local data has been cleared entirely and I am back to my landing page. I have tried the standard and pure local storage method as well and yet nothing seems to work.

We use it and have app settings persist for weeks without any issues with this library

We use this :

Works without problems on ios & android.

Still want to stick to localStorage for the sake of simplicity however the data is still randomly cleared out. i could close the app and go into most notably Flipboard for a few minutes, reopen my app and the localStorage has been cleared entirely on ios. i have tried every single possible solution regarding LocalStorage and to no avail which is beyond frustrating at this point. What could possibly be the issue so I could get simple login data to finally persist!

can you post your angular-cache configuration or code? I am using angular-cache to persist to localStorage which never gets cleared unless I uninstall the app

What I have noticed is that the localstorage persists if there is memory on my iOS device. However, if it’s at zero mb, the device will purge the localstorage from my app. How do I avoid the storage being removed if the device is low on storage?

I dont think you can @radbeta . That is why it’s not recommended to have your app depend on localStorage, but use something a little more robust like SQLLite, although I’m not 100% sure you wouldn’t run into the same issue with low device storage. Might be something to research

I’m facing the same issue, but it happens after more than 4 hours. While my mobile has enough space free…
Did anyone of you find any solution for it?

Its most certainly just like @radbeta described, low internal memory on iPhone. It that case iOS automatically delete some caches and localstorage.

Use sqlite.

There is no way to guarantee that local storage will persist.

LocalStorage works well if there is sufficient memory on the device(usually more than 20mb at least) though depending upon what Backend database or service you may be using, you could always check if the user still has permission to access your service if their login token has not expired. Firebase does an excellent job of providing a few line snippet making it seamless to check and edit the expiration length of the token in the dashboard as well as providing user login through various services such as Facebook, Twitter etc. If LocalStorage is what you need, be sure you don’t store your data in a Directory that won’t persist or that iOS or Android purges. Storing your data in the Documents Directory gives you the best outcome and avoidance of the data being wiped arbitrarily though it’s not a guarantee.

@radbeta We are having the exact same issue! What exactly did you do to persist local storage data? We use Firebase as our backend and Firebase automatically stores the auth data (w/ expiration token & other info) in local storage. But when users don’t have sufficient memory on their phone, local storage is wiped out and the users are logged out.

How exactly do you suggest persisting firebase auth data that was stored in local storage? Thank you so much for taking the time to solve this issue; we really appreciate your help!

If you haven’t yet figured it out, reference the links below. They all achieve what you are looking for with auth and session persistence while be exempt from data purges when the device is low on disk space.

(encrypts data and auth data is sensitive so this may be best)