I installed Native Storage
npm install cordova-plugin-nativestorage
npm install @ionic-native/native-storage
ionic cap sync
but now when I run
this.storage.getItem('user').then((d) => {console.log(d); return d;});
in the browser it fails with
ERROR Error: Uncaught (in promise): cordova_not_available
?
In my ionic3 version of the app that I am porting to 5 I was able to run native storage / a storage in the browser.
NativeStorage != Ionic Storage.
NativeStorage stores stuff using OS-dependent lower-level primitives. Ionic Storage is much more portable, and is likely what you want if you need to fail over to localforage in a browser environment.
Thanks, but why is local storage not listed in the native api documentation?
I get that it’s confusing to have many things with similar names, but:
-
localStorage
is a DOM thing. As poker players say, it’s dominated. In any situation, there’s some better choice than this.
-
localForage
was originally developed by Mozilla to address the biggest shortcoming of localStorage
: it’s not very reliably persistent and gets wiped out from under you at unpredictable times. So it has a similar API as localStorage
but uses different and more persistent storage methods under the hood. It’s a decent choice for many situations, but…
-
Ionic Storage is a better choice for Ionic apps because it not only wraps
localForage
but also adds seamless support for SQLite if desired. It should be your primary go-to for storing persistent stuff in an Ionic app, especially if you need to support desktop browser environments without additional work on your part.
-
Native Storage we’ve talked about. If you need to share stored data amongst many different apps on iOS, this is the best way I know of to do it. Its downside is that it needs Cordova (may work with Capacitor, I haven’t tried).
Yeah, thanks @rapropos . In my ionic3 app I in fact used ionic storage. But, now that only Native Storage is listed on the seems Ionic5 default API documentation I thought had to use that one, ionic storage seems not listed so I thought it was not supported anymore.
I’m sorry about the confusion, but it’s not really clear from the current documentation how to properly install ionic storage with capacitor (to me, maybe I just do not see the link in the docs). On the GitHub page you shared it’s using the cordova installation commands.
Only for the (totally optional) step of using the SQLite driver. If you are happy with the localforage driver - and I don’t know of any compelling reason not to be - breeze right through ignoring that step. Just npm i @ionic/storage
and do the stuff in the Usage section.
Ok, thanks for the help. I’ll do that. Yeah, I just need to store JSONs, so local forage would be enough.