How to initialize an iOS app with default data? - ios

I'm writing an app that needs to access data from a large file every time a button is pushed. I've been reading up on it and the apple documentation says:
"You can create a separate persistent store that contains the default data and include the store as an application resource. When you want to use it, you must either copy the whole store to a suitable location, or copy the objects from the defaults store to an existing store."
Does this sound like the best way to go?
I've created the database with the table I need and put it under "Supporting Files" in Xcode - is this an application resource? Also I'm not sure what it means by "you must either copy the whole store to a suitable location" - is this not it?
Finally, my main question - how do I access the information in the DB in my .m files? Thanks for bearing with me, still very new to this.

Does this sound like the best way to go?
I believe it depends on what kind of information you want to access and if you need to update this information (add new information, modify it, delete, etc).
If you just want to read the data and do something with it, check out Property Lists and XML. In case the data is structured somewhat like a relational database and it is necessary to modify it, check out Apple's Core Data framework.
Is this an application resource?
Yes, it kind is, don't worry about the nomenclature so much but for more clarification check out Apple's own guide about resources.
Also I'm not sure what it means by "you must either copy the whole store to a suitable location" - is this not it?
You can load the store from multiple locations (folders, different files, etc), this just means that you should pick the best that suits your application.
Note, the files you import to your project are stored in the application's bundle and you can't (as far as I know) modify them. So, if you choose to include a Core Data store file, you need to copy the store from the bundle to a location of your preference (ex: the documents folder).
How do I access the information in the DB in my .m files?
It depends on your pick. As it looks like you're more interested in using Core Data, start by looking at the link I've provided above and searching some tutorials.

Related

iOS - save data so that it is easily accessible outside of the app

Is there any way to save a file (to be exact, a .csv) somewhere outside the application sandbox? From what I've read it seems like it is only possible to save files inside the sandbox.
Maybe I'm thinking about it the wrong way, maybe I don't really need to save it outside the sandbox. What I actually need to do is:
Allow the user to save the file, so that he can use it in other applications (for example email client), and it would be best if the file would stay on his device after he deleted the app. The location of the saved file doesn't really matter, it could be chosen by user with some file picker or hard-coded. Is there any way to achieve this?
I'm using xamarin.forms so solution easy to implement in xamarin.forms would be much appreciated.
From what I've read it seems like it is only possible to save files inside the sandbox.
That is the general idea. You don't just have access to the filesystem on iOS. You could work around this by maybe saving it to iCloud or Dropbox, etc. depending on your use-case but basically you can't just save it somewhere common. Normally, you would then implement the 'share' functionality. What happens then is that iOS copies the file into the other apps' sandbox so it can work with it.

Where to save my iOS app's save files

I am learning iOS file system to store my game data like saving the amount of money earned and things like this in a xml file.
The question is where should I store them?
I read this: File System Basics
And I don't know that I should save data in Documents directory or Application Support directory.
Because the data I want to save is something that is not associated with user's documents and user will never see it so it should be in Application Support, but on the other hand it's something that is affected by user interactions so it should be in Documents.
I think it is the Application Support/MyGame as per the Apple Developer guide link shared by you. You are confused by the fact that it should not be something affected by user interaction. Actually it means user should not directly interact with these. See this from the link: File System Basics
Using something like Parse may slow down your app as it needs to interact with their server, but you do get a backup.
For something of this size, I'd use a mobile backend solutions provider like Parse or Firebase. Personally I use Parse in my projects and is very easy to implement advanced functionality into your apps like user registration/save data to users.
Hope this helps.

Where to store user settings in Electron (Atom Shell) Application?

I can't seem to locate a built in mechanism to store user settings. I was hoping that electron provided a standard method for storing user settings across all desktop platforms. If there isn't a precedent for this I can implement it myself, I just didn't want to jump to a custom solution immediately. Research online is pretty sparse in this area. Thanks!
Each platform has different default locations for different kinds of data. So, if you want to store data in default locations based on platform, check out app.getPath(name)
It retrieves a path to a special directory or file associated with name.
You can also use it to differentiate between data the user wants to save, and data your application saves that you don't want to clutter up users directories.
Or if you just want to store files reletive to a specific path you can use the
app.setPath(name,path)
I've faced this particular problem with my Electron app and this post inspired me to write an NPM module called electron-json-storage.
This module allows to easily write/read JSON to/from app.getPath('userData'):
const storage = require('electron-json-storage');
// Write
storage.set('foobar', { foo: 'bar' }).then(function() {
// Read
storage.get('foobar').then(function(object) {
console.log(object.foo);
// will print "bar"
});
});
Electron doesn't give you anything out of the box for this. However, Electron does give you a method for getting the idiomatic location of storing user data in a cross platform way via the app.getPath API.
I'd say the 3 most common ways to do this are:
localStorage (or any HTML5 storage API)
flat JSON file (this is what I do, and I use electron-store for it)
embedded database like IndexedDB, neDB, or sqlite
Which one you choose will depend on your app's needs. If you only need to access this data in the renderer process, then I'd just use localStorage. Most of the time it seems you need to access the data in both the main and renderer, so a JSON file makes sense. If you're dealing with lots of data or complex querying, then maybe a database makes sense. I wrote about this more in detail here.
How about LocalStorage? If you need to access these settings from the browser process, you probably need to write your own (or just use a node.js library that implements this)
The best way that I have found is to store it in a simple file as JSON. The problem is that if you store that JSON in the app dir, then when you update the app, it will get wiped out. So you want to put it in the default directory for user settings for the current operating system. LUCKILY!!!!! There is a library for node developers that will help you find the userdata directory. The module is called appdirectory, and I have used it several times. It is extremely easy to use.
See APPDIRECTORY HERE
One could store data in cookies; Electron has a mechanism for it (https://electronjs.org/docs/api/cookies) and the cookies can be retrieved in the browser (Angular: https://docs.angularjs.org/api/ngCookies/service/$cookies, React/Other: https://github.com/reactivestack/cookies)
I was able to get it working with Angularjs.

Persisting app configuration in iOS: .plist file vs NSUserDefaults

I have some app-related parameters (boolean and string data) which are either set by user through the app, either at its first execution. I don't want this kind of information to be stored by using Core Data, so the options I'm thinking about are:
1) Creating and reading/writing a custom .plist file (let's say, myconfig.plist) to be stored in Documents app folder
2) Saving such info using NSUserDefaults
The kind of information I want to keep is, for example, the last app version that was installed and run in order to check if app is being updated, or a flag telling if some specific set of data has been already loaded without having to check the model.
Which of the options would be the most suitable and safest for this task? Or is even there any better option of handling this?
Thanks
try this if you would check appstore version with your actually app version https://github.com/nicklockwood/iVersion

Where to store and access additonal content for iOS app from own server

I am trying to understand something that is not clear to me about the storage and access of additional content downloaded from my own server to my iOS app. Clearly this is possible, see the section called Downloading Content from Your Own Server . I understand how to process receipts and how to make a call to my server. What is not understood is:
When I initiate a download from my server what file type does the download need to consist of? Can it be anything I want?
Where I am a supposed to store it on the device considering the app bundle is not allowed to be updated?
How does my app access the data?
In the simplest of cases, say I am delivering an xml or json file I guess most of the above would work like this: store the file in the documents directory, use filepaths in the app to access and load the data.
However what if it is something more complex that includes say images and other data that should be accessible just like other resources in the app bundle are accessible?
The crux of what is not clear to me is what type of data can be delivered, if it is anything I desire (which I presume it is) and I have complete control of the data type, where should this data content be stored and how can I enable my app to access this data as seamlessly as possible. For a concrete example suppose I am making a game, the first level is included in the app bundle, I deliver the second level via my own server, now, can I load that level the same way I load the one in the app bundle?
If there is a link or documentation I can pointed to that would be helpful but I have not found anything just yet, perhaps it is the concept of the app bundle that is most confusing because on a regular program I can do whatever I wish and things are not so unclear.

Resources