quick question about plist - iphone

Can my application download a .plist from a URL and replace the one that I built in xcode and shipped with the application?
Thanks
Leo

No, you can't change anything in your application bundle. You must download you plist file to some folder in application sandbox (documents or caches) and read it from there.
Your possible workflow for reading that plist can be:
Check if plist file is present at download location. If yes - read it from there
If it is not present - read plist shipped with your bundle (or copy plist file from bundle to download location and go to step 1 - this way workflow may be a bit more consistent)

You can use an NSURLRequest to download the .plist file, and then save it the Documents directory in your app's sandbox. Use the NSSearchPathsForDocumentsInDomain() function (see the Foundation Functions reference for more info) to get the file system path to the Documents directory.
Read for More

Related

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

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

How to copy a file from Documents Directory to application bundle?

Hi in my app launch i will invoke a web-service call and i will download some zip files now i need to save those files permanently in my application bundle so that when i launch my app for the second time i will just look for update if there is no update i will just use my previously saved files, so i thought to make use of document directory and get the zip file then unzip it and save it in application bundle, here my problem is how to save a file from DocumentsDirectory to my Application Bundle?
and does my approach is right & efficient way or do i need to follow some other ways to achieve this? Any help is thankful in advance.
You can't write to your application's bundle - it is readonly, only the documents directory is readwrite.
You don't need to move the files, as the documents directory provides permanent storage.
The resources which are modified at runtime should be part of Documents directory. iPhone application creates a sandbox environment which is signed. If you try to modify any of the bundle resource or try to add/remove the resource, it will not allow. It works fine with simulator but not with device.
So you should store your response to the documents directory and not to the bundle.

Save and overwrite files in the resource folder possible?

Do anyone know if it is possible to save and overwrite files to the "Resources" folder which I create for my resources before sending the app to Apple? So I know how to get files from there, but I'm not sure how to save and overwrite for e.g. a pdf file from a specific url to this folder, when the App is on the Iphone.
Thanks for your answers!=)
You can't make any edits in the Resource folder after you app is shipped,It's read-only,use Documents/Caches/tmp instead.

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.

Is it possible to modify/update the plist file programmatically without replacing it?

In my application
I want to change only some content of the file.
For doing that I am fetching the plist file in dictionary.
Modifying the dictionary and then Re-Writing the whole plist file.
Is it possible to modify the plist file directly or some content of the plist file.
It's not possible to modify a plist file without rewriting the whole file. If your plist file is so big that it takes too long to rewrite the whole thing, you should look at using a SQLite database instead, and perhaps adopting Core Data.
If you are talking about Info.plist or any resource inside your bundle (i.e.: inside MyApp.app folder) the answer is NO.
Files inside your bundle are read-only. So you cannot override them.
If you are talking about other files that could be in Documents or Cached folder then the answer is YES.