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

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.

Related

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.

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.

How to store the sqlite3 database file in the Library directory of an app?

Pretty much everyone and every tutorial seems to store it's private app data in the documents directory.
But in my image editing app I want the user to be able to use iTunes file sharing. The problem: It exposes everything in documents to the user, and he can accidentally delete important private app data folders like the SQLite database.
Apple recommends this:
If you do not want files to be shared
with the user, put them in your
application’s Library directory. If
none of the standard Library
subdirectories are appropriate, create
a private directory inside the Library
directory and give it the name of your
application’s bundle ID. You can then
use that directory to store any files
that are private to your application.
I have never heard of that Library directory. Now the question is: How can I access this directory? Where is it? Will it be backed up, just like the documents directory when the user syncs with iTunes?
Use NSLibraryDirectory in lieu of NSDocumentDirectory (in those examples you speak of) to get the library directory and then create a directory in there using NSFileManager.
Read this for more information on the application directory structure. The Library directory is backed up except the Caches directory.

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

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.