Hi,
I’m using App Preferences within my app and it works great in both the Android Virtual Device when debugging or when installed directly through APK.
For reference: https://ionicframework.com/docs/native/app-preferences
Nevertheless, when I publish my app to Google Play and install it through Google Play, App Preferences does not work. It never save nor load the preferences being set into the app.
Any clue what to do to resolve this issue? could it be a permission problem?
The code I use is pretty simple via Angular and latest version of Ionic:
Instantiate:
constructor(
private appPreferences: AppPreferences
) {}
To Set:
this.appPreferences.store('MyParameter',ev.target.value);
To Load:
this.appPreferences.fetch('MyParameter').then((res) => { this.MyParameter = res; });
Thanks for your help.
Things I would check:
-
Try the fully signed, release build on your device BEFORE uploading to Google play.
-
Check for the existence of the Cordova plugin on your page, not just assuming it’s definitely available.
-
Ditch the Ionic Native wrapper and use the plugin directly, to rule out that as the cause.
I’ve had this several times with other plugins and these steps usually find the issue.
@Daveshirman thx for your suggestions.
I ended up debugging the signed version using console.logs messages and logcat.
Conclusion: App Preferences work fine, but do not behave same between signed and unsigned release.
The app I’m building has 2 pages/tabs. The AppPreferences are set on tab2, and meant to be loaded on tab1. Each page instantiate AppPreferences.
In the unsigned release:
- tab2: loads and store AppPreferences without any issue
- tab1: loads AppPreferences (set in tab2) without any issue
In the signed release:
- tab2: loads and store AppPreferences without any issue (SAME)
- tab1: failed to load AppPreferences (set in tab2)
Any clue as why it behaves so differently between the 2?
Any workaround to make sure preferences loaded in tab2 can be passed to tab1 (and also loaded when starting the app)?
Thx
I would look at it another way. Why don’t you use something else? SQLite for example ?
I mean if you just want to store something and pass it around, there are several options.
Went down the storage API path instead of AppPreferences plugin, and I ended up using storage API available for capacitor - pretty straight forward and working perfectly:
Thx @Daveshirman for your suggestions.
If anybody’s still reading this “marked solved” thread, I would like to plead:
Don’t use external storage, whatever plugin or library you use to access it, whatever place you actually do the storing, to do in-app communication. For communicating amongst parts of a running app, use a service provider that is injected by both writers and readers.
The proper role for external storage is to talk to the next time the user opens the app.
There are a few reasons for this:
- Any external storage API worth using operates asynchronously. If you are writing in one part of your app and reading in another, you now have a bunch of potential race conditions to worry about.
- It becomes categorically harder to change your external storage system when the code that touches it directly is spread throughout your app. Draw lines of delineation of responsibility where you can, and this is a natural one.
- ReactiveX does a great job of proactively transmitting data changes. This is easy to leverage in a mutually injected service, not nearly as much so when dealing with an external storage system.
@rapropos thx for your comment, and I confirm that in my use case, I need the “next time the user opens the app” - this the reason I was initially looking at the AppPreferences plugin.
1 Like