Swift Filled plist is empty next time I open up the application - swift

When I'm opening up my application for the first time I have an empty plist that I fill with some values (writeToFile). I can read from the plist dynamically and get those values I just put into it. This far this good. Then I close my application and later on I open it again. At this point I want my plist to contain those values I dynamically wrote to it the previous time I opened the application, but to my disadvantage it's empty.
Is this a normal behaviour? Can I not use the plist as a "local database" where I save values dynamically, and read them some other time when the application is opened?
Thanks!

Files in "supporting files" are not supposed to be modified.
The files of the supporting files group are (like all other non-code files) copied into the app bundle. Apple says about the app bundle:
This directory contains the app and all of its resources.
You cannot write to this directory. [...] Writing to this directory changes the signature and prevents your app from launching. You can, however, gain read-only access to any resources stored in the apps bundle.
If you want to store users preferences, selection, etc. you should create the plist file in the documents directory.
Use this directory to store user-generated content. The contents of this directory can be made available to the user through file sharing; therefore, his directory should only contain files that you may wish to expose to the user.
The contents of this directory are backed up by iTunes
You can get the path to the documents directory using:
NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last as! String
If you want to store preferences you may also want to take a look at NSUserDefaults.
https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

Related

Whats the difference between saving content at document directory or temp folder?

I want to save multipel photos in my application, so that application run in background.
So what is the main difference between saving photos in doc dir or temp path.
And suggest me which is best way to save photos
Document Dir
Temp Folder
NSUserDefaults
Thanks in advance
Here is a reference: File System Programming Guide.
Temp folder:
Use this directory to write temporary files that do not need to
persist between launches of your app. Your app should remove files
from this directory when it determines they are no longer needed. (The
system may also purge lingering files from this directory when your
app is not running.)
Documents folder:
Use this directory to store critical user documents and app data
files. Critical data is any data that cannot be recreated by your app,
such as user-generated content.
Usually, I put files in temporary folder only when I cache something and I don't care if these files will be deleted. If I want to be sure these files should live long life, I put them to documents folder.
The main difference is the path: <sandbox>/Documents or <sandbox>/tmp.
Some more differences:
The Documents directory can be accessed via iTunes if your app has file sharing enabled.
The contents of the tmp directory is volatile, the OS is free to purge it in order to save space.
About NSUserDefaults: that's something completely different, it's a mechanism which stores app-specific configuration data in property lists, I can't imagine how and/or why you would use it for storing images.

Newsstand App Storage

I have developed one app in which, monthly magazine issues are downloaded and stored inside the app Document directory.
But app have rejected app due to storing of magazine issue in document directory. My magazine file size is around 50 MB.
They mentioned below:
The iOS Data Storage Guidelines indicate that only content that the user creates using your app, e.g., documents, new files, edits, etc., may be stored in the /Documents directory - and backed up by iCloud.
Temporary files used by your app should only be stored in the /tmp directory; please remember to delete the files stored in this location when the user exits the app.
Data that can be recreated but must persist for proper functioning of your app - or because customers expect it to be available for offline use - should be marked with the "do not back up" attribute. For NSURL objects, add the NSURLIsExcludedFromBackupKey attribute to prevent the corresponding file from being backed up. For CFURLRef objects, use the corresponding kCFURLIsExcludedFromBackupKey attribute.
Any help appreciate.
Thanks.
Move your files to the Caches directory and you should be good to go.
I believe that if it is user generated content, there is no problem with storing it on the /Documents folder.
Since it is content that can be downloaded again, you have to set the NSURLIsExcludedFromBackupKey file attribute which prevents it from being backed up to iCloud.
Take a look at this question on setting the attribute: Use NSURLIsExcludedFromBackupKey without crashing on iOS 5.0

Why copy a plist from the resources to document?

This is what is my understanding: the resources in the project folder are read only. So, almost all examples show copying a plist from the resources to the app's document folder. Why can we not simply find the app's document folder (after first run) and create the initial plist there (i.e. in the documents folder of the app so that subsequently we can modify the plist via code?
The answer is: Yes, you can create an initial plist there. But before you do this, consider using NSUserDefaults to save the settings.
Because there might already be data needed in a plist when the app is first installed; and that data has to come from somewhere. Why not from a plist?
I would also suggest using the Library folder rather than the Documents folder, just in case you do file sharing via iTunes at some stage. The Documents folder is available to the end user, whereas the Library folder is not.

Shipping Documents Items with an iPhone App

My iPhone app uses a small database to store its settings and saved files. How can I ensure that the default database, with its default settings, gets distributed to anyone who downloads it along with the application files?
EDIT Sorry, I was in a rush when I posted this. I forgot to mention that the database needs to end up in the 'Documents' folder of the application so that it can be backed up at a later date by the user.
-Ash
Put it in "Resources". Then on your first launch, you'll need to load that file out of your mainBundle and save it to the Documents directory. The file will "come with" the app, but it won't live in the right place to get caught by backup.
A side-effect is that restoring the app to factory settings is as easy as deleting that file. Next launch, you can see you don't have your file, and copy a fresh one out of your bundle.
You include it as a file in the Resources folder of your application.

Replace existing XML file within iPhone app

I have an .xml file that is going to be shipped within my app.
This file contains values that are read from it and saved as an array when the app launches.
Each time the app is run, I want to check with the server if there is an internet connection. If so, I want to get the newest version of the .xml file from the server and replace the one that I currently have saved in my app (this way, the next time the user logs in and doesn't have internet access, he/she will be able to use the old (yet most up to date) data).
What is the best way to do this?
Thanks,
The best way to probably do this is to copy the XML file from the app bundle to a location in the app's sandbox, e.g. the Documents folder. Thereafter you can update the XML content as necessary with newer data from the server. The copy is necessary to allow you to write to the file, since you cannot change the content of your app's bundle because it is signed.
Alternatively, if the data is simple enough, you can just save it to user defaults on first launch and change the defaults on subsequent updates
I might skip the XML altogether, unless it contains a baseline of default settings, and just sync user defaults over the Internet. You can't modify files in the bundle, so your only option would be to copy over a "default-settings" XML file to the application's Documents folder to make it editable.