Can I create a Documents directory in Xcode, and put files in it for my iOS app through the UI. If so, how? - ios

I need to read/write a tiny amount of info for data persistence in an iOS app. I am using plists and understand that I can read, but not write to the bundle. If I create the plist files initially at development time, how can I place the plists in the "Documents" area of my app so that they are later writable?

Short answer: No.
As you say, an iOS app's bundle is not writable, but it is readable.
Further, an app's documents directory doesn't exist until the app is run (on the user's target device) for the first time. Thus, you can't build your app's documents directory at compile-time, nor can you submit an app to the app store with a prepopulated Documents directory. (It would be nice if Apple gave us the ability to provide a "seed" documents directory that got moved to the documents directory at first launch, but they do not provide that facility -- at least not that I'm aware of.)
What you can do is to create a sub-directory of your bundle that contains a set of files that you want to place in the app's documents directory.
On launch, check a "hasBeenInstalled" flag in UserDefaults. If it's true, do nothing. if it's false, use the file manager to copy files from your app's bundle into the app's documents directory, then write hasBeenInstalled=true to UserDefaults.

Related

Need to place assets inside Library folder of an application's Sandbox for iOS app

Generally if an application is build and deployed, In sandbox location four folders will be created which are listed below.
1. Library
2. temp
3. Documents
4. xyz(Application's Root folder name)
But all the resources are placed inside 4th folder by default in sandbox location.
Is there any way to place selected file inside Library folder or any other folder in the above list while building application.?
No. Building an app can only put files in the app's resource bundle. Remember, when a user downloads your app from the store, all they get is the app. When the app is installed, the app's sandbox is created for the first time. But the sandbox is empty except for the app's resource bundle.
If you want files to be put elsewhere in the sandbox, your app needs to copy the files from the resource bundle to the other locations the first time it is run.

Removing cache files when app is removed ios

I'm wondering about removing cache files I've stored inside my app. The directory is made in Documents, beyond the app's folder. Is it possible to write code inside app that will be executed when iOS removes the app from system? Can it be done without storing files beyong app's cache directory?
Thank for any answer. I'm working in emulator so what path should be to make directory in app tmpFolder to not take care about leftovers after deleting app?
you aren't informed about an app's deinstallation / removal BUT nothing except iCloud and keychain survives the removal - especially not some app's caches
your app runs in a sandbox with its own library, caches and documents folder

Store images in iOS app directory when install

I am writing an iOS app which download images from a backend server, and store them in the document directory. I know how it is done.
But I need to write a prototype first, which use some pre-given images (not retrieve from the backend server). My question is, is it possible to store images in the document directory during initial installation of the app? I don't want to hardcode them in the bundle directory.
Thanks
Nothing can be done when the app is installed, only when the app is run. The first time the app runs, you can either copy pre-bundled images to the other directory (Documents) or you can download them as needed. It sounds like you want the first option for the prototype.
Basically you need to do what you don't want to do - hardcode them in the bundle.
Remember, the app's sandbox is setup when the app is installed. This includes the creation of the Documents directory. But nothing is put there. Only your running app can store something somewhere in the app's sandbox.

Where to store music?

I'm developing an application that downloads music from the web. I now need to upload it to the app store. But I don't know what folder I should save loaded music in. It says that for files downloaded from the Web, I need to use Library/Caches directory, but iOS can remove files from this directory. I don't want that to happen.
Store them either in the Documents or the Library directory. (NSDocumentsDirectory and NSLibraryDirectory constants, respectively.) iOS doesn't remove the files stored in there, as these are not temporary (tmp or Caches) paths.

retain program data in iOS app while updating and sync to iCloud

When a new upgrade is released and installed on iOS, which folders / files are left untouched by the upgrade process? There are several folders; Library, Caches, Preferences, Documents.
Normally Application Support Directory is not created by default. What happen to AS folder during upgrade?
I learn that all data in Documents folder will copy to iTunes or iCloud by default. Is that true?
If I create my own CoreData db to persist, where should I keep?
Your best bet is to read the first half (which pertained to iOS) of this document from Apple:
In my experience, I put Core Data db in Documents folder if the iTunes File Sharing feature was not enabled on your app. Otherwise create a folder in the Library folder called Application Support or anything name, etc. With iTunes File Sharing enabled, user can accidentally delete any files in the Documents folder.
During the upgrade, the contents of Library and Documents folder are retained.

Resources