Replace existing XML file within iPhone app - iphone

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.

Related

Best way to remotely update app settings in iOS

I am working on a application in which i have to update all app related settings remotely.
App setings include
Text Font
List item
Text Color
background Color
Images
Url etc..
My idea is something like this, the first time when the user install the app it start downloading all images and settings like text color, font etc.
And when ever any changes happen the app itself downlaod those changes and store it locally.
Currently i am using a constant file where i have written all required settings.
Tell me some best way to achieve this and also tell me where to save the settings.
If is there any sample app available please provide me the link.
Thanks.
You could try something like GroundControl. It updates values in NSUserDefaults from a file found on a web server.
If you have a constant file from which you read all your settings, you can place an identically formatted file on a server, (maybe have a "last updated" date saved inside these files). Upon every session init, try to download the remote settings file - if it is "later" than the one you have, or any settings is different than the one you have saved, save the newly downloaded file instead of the old one, and act upon the changes.
The settings' flags and vars may change instantly across the app, while downloading images for skin and UI occurs immidiately after this, and take effect after they have completed downloading (which may look awkward to the user) or, more plausibly, upon init of the next session.
IMHO, using NSUSerDefaults settings and downloading a remote plist with the same keys and structure is most convenient file format for settings stuff.
Also, you may place the remote file in a web service, and have the client send that web service the request for the file accompanied with a "my last updated settings are of the date X" - the date of the latest settings you have locally. Then, the server may return a file, or an "you already have the latest file" response.

Write a File into the App Bundle

I have a Webservice to download some News and write them into a plist-file somewhere in NSLibraryDirectory, thats fine. But if the user has no Internet connection or the webservice is offline or whatever, I load a Default-Newsfile from within the AppBundle.
At the moment I have to replace the Default-File manually before every AppStoreUpdate to keep it up to date.
My Question: Is there an easy way to write into the App Bundle while debugging via Simulator, so the Default-File will everytime be up to date.
I tought about something like:
#ifdef SIMULATOR
//Write to AppBundle
#endif
Note:
It's all about the time while I use the Mac and the Simulator, I don't want to do that in the Published App!
Yes, I already replace the Ressource in my Bundle, but by hand and I want it to realize automatically.
I want to keep my Projectfolder up to date, before I compile it for any Device or the AppStore.
AppBundle is Read-only you cannot write anything to it programmatically, however if you update the resources in your Project through Xcode, you get the updated file automatically in the AppBundle..
Ok, based on your comment, here's what you need (I think - still not completely clear on the question, but let me know if this works for you).
Create an empty file (say news.plist) in your project folder (SRCROOT). Add this to your repo.
Add this file in your project's resources and add it in Copy Bundle Resources step in Build Phases
When you download content from internet, save it to project's temporary folder, then copy it to SRCROOT/news.plist overwriting existing file (optionally add a check on file checksum to avoid unnecessary change). This step is required ONLY when running on simulator.
Whenever developers check-in, the updated news.plist should be checked in to the repo.
To make SRCROOT available in code, add SRCROOT=\"${SRCROOT}\" to GCC_PREPROCESSOR_DEFINITIONS.
Other developer won't need to do anything except the last step - check in the updated file every time it changes.
Does this help?
No, you might be possible to get the simulator to write to you App Bundle, but this does not mean that the file your project (the one that gets compiled when you build for the app store) will be updated.
Why not just save the new to the documents directory, then always load this file. After the app is started, start a background thread and try to update the plist file. Of you are successful in retrieving the news over the write the plist file in the document directory.
This way the user will have an file that was retrieved the last time the device was able to get the file.
You could add an file in the app bundle which you copy to the documents directory on first start of the application, just to make sure that the user has data in the app.

iOS - Allow user to download file from documents directory but not to upload file using iTunes

Here is a situation I have a data file in document directory which is being updated in the application every now and then. So I want to save it to my desktop using iTunes. But I don't want that the file should be uploaded back to my application. i.e. I want that user can download the file but can not upload any.
I was thinking to have the data file on some other location like Library and put a button on application settings saying "Prepare backup" that will copy that data file in Document directory, from where user can download it. If user uploads any thing it won't make any difference as my current file is in Library directory.
This is just a thought,
can anyone suggest me other way or the above way is good to go?
Edit: I just need that after the successful export user can view the data file (may be later) without support of the application.
You can't.
But what you can do is check (using an timer every 10 seconds) if a new file is added to the documents directory and then delete it programmatically.
But this ofcourse doesn't disable the replacement of files.

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.

Where does the sqlite database for iPhone is stored with the application

I want to know when we install an application where does the database of the application resides. Like Does it resides in the application bundle or sandbox.
Also when we upgrade it why does not it affects the existing database. For example, if I make some changes to database table and reinstall it, it still uses the previous table. So how does it actually works at the background.
You choose the location by providing a path to a db file when you call sqlite3_open. The path should almost certainly be to a file in your Documents directory, since any place else either won't be backed up (tmp) or won't be accessible (the app bundle, or paths outside your sandbox).
Since you manage the file, you could also create a .db file on your computer with the default database contents, put that in your app bundle, and then copy it over to Documents the first time your app comes up and finds no file in the expected location.