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

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.

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

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.

quick question about plist

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

Where is the SQL persistent store created by CoreData?

Can someone help me find where CoreData is storing the SQL file it creates in iOS applications?
So far I've tried "Show package contents" on the .xcdatamodel file and also the .xcodeproj file...No dice!
Is there an editor for looking at the contents of these SQL files?
Cheers.
-A
The url parameter of [NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:url:options:error] specifies the location of the file. Find this call in your code and see where you've stored it. XCode templates will put it into the top of your application documents directory, but you can put it anywhere you like.
Note that Apple considers the contents of this database to be opaque and non-user-modifiable. Playing with the data in your store is likely to cause issues with Core Data.
It's typically saved into your application's documents directory, with the name of your data model.
So, for example, if my data model is called AppData.xcdatamodeld, it will be AppData.sqlite inside my app's document directory. You shouldn't really need to touch the .sqlite file though.
Hope that helps!
Core Data editor --> http://christian-kienle.de/CoreDataEditor/

Property List vs XML in iPhone

I have to save some config parameters with values in iPhone. I need to edit values at app runtime. I see there are two ways to implement this:
1. Use pList file
2. Create new XML file.
What is the best approach to implement this?
Should I use existing Info.pList file?
Thanks
The best solution is probably using a dictionary and saving that to a property list, it's incredibly easy. Do not use the existing Info.plist, you don't want to polute it with your own settings.
Here's a quick exaple on how to save a dictionary to a property list and load it:
http://codersjunto.com/wp/?p=16&cpage=1
An Info.plist file is XML, it is directly readable from a file to a NSDictionary or NSArray. You can also choose the file format as XML or a binary format, the binary format is a lot smaller and can load faster especially over a network.