user1791283 user1791283 - 2 months ago 31
HTTP Question

ionic 2 Overriding local file via http plugin

i'm building an ionic 2 hybrid app which gets a settings.json file via http.get('build/settings.json') on startup. I will give the client the permission to update these settings. At the end I want to update the file with the new user settings. I don't want to rush into the File Plugin from ionic-native (cordova) : https://ionicframework.com/docs/v2/native/file/ just to save this file (because it lacks of Support on Windows phone OS).

I couldn't find anything online about this concern.

Answer

Well, what your'e trying to do is not really applicable nor recommended. You can fetch the settings JSON from the build directory, but you can't edit them. Further more, you can't save the settings to the wrapped-up build folder. The only place you can write files to is actually the folders specified in the cordova-file-plugin, and as far as I know, the integration is not fully implemented on the ionic side. In addition, if you do manage to write those files in to the device file-system, you can't fetch them like a normal json because you can't perform http requests on files.

On the other hand, you can use a local database for your app. It could be easily done using Ionic's Storage, alongside with SqlStorage which let's you save persistent data on your device. By that, you can easily implement a service that gives you full access to the local database, for reading, updating deleting etc, using simple SQL Syntax.

This is a much simpler way to achieve your goal, and for no doubt a much much better practice.