Can I make my iPhone app start with a file in its Documents directory? - iphone

During development, I'd like to have an XML file in my iPhone app's Documents directory when it starts. Is this possible? Having read this answer, I tried adding a Run Script build phase, but I can't find an environment variable that points to the application's home directory after installation. To be clear, the directory I want to move the file to is here, in the case of a Simulator:
/Volumes/Stuff/Users/johnDoe/Library/Application Support/iPhone Simulator/User/Applications/118086A0-FAAF-4CD4-9A0F-CD5E8D287270/Documents
Or here, in the case of a device:
/var/mobile/Applications/30B51836-D2DD-43AA-BCB4-9D4DADFED6A2/Documents
It won't do just to copy the file there manually, because I want to send the source code to someone else so they can see it, and I don't want them to have to do any set-up before they can run it. If what I'm asking isn't possible, then I'll look at ways to move the file programmatically after the app has launched, but I thought I'd check with you guys for a neater way first.

Do you need to have the file in the Documents directory, or do you just need to have access to it from within the app? In Xcode your current target should have a "Copy Bundle Resources" build phase. Add your .xml file to this build phase and you'll be able to access the file at any point in your code with the following call:
[[NSBundle mainBundle] pathForResource:#"my_file" ofType:#"xml"]

Related

Imported packaged files ".key .numbers .pages" from iCloud using UIDocumentMenuViewController are not opening in UIWebView

Hi I am trying to display a packaged file say .key/.pages/.numbers file to user in a UIWebView but I cant. The keynote file was created using a Keynote app from my iPad recently. I imported that file to my app using UIDocumentMenuViewController from iCloud. I tried to look into the file attributes and it turn out that the file imported is actually a "directory" not single file. Inside, it has some contents. Can anybody tell me how to go from here?
I had the same problem. iWork files are zipped bundles but for some reason importing from iCloud unzips the contents first, whearas copying from the app directly via Open in ... from Number (or Pages or Keynote) works perfectly.
So what you need to do is test if the result of import is a directory, and if so zip the directory into a single file and keep the correct file extension (.number pages or .key).
This seems like a hack but it works and is best workaround I've been able to find
In Swift you can use SSZipArchive as an easy way to zip a directory

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.

iPhone Application Support Folder Include Files BEFORE Building

I have an iPhone app that programmatically gets a path to the Application Support Folder, tests for a file in the application support folder, and then either loads the file or creates a new one depending on the result. This is easy and there are a ton of tutorials on how to do this.
But I can't for the life of me find anything in the ios documentation or online about how to put a file in the Application Support Folder before ever building the app. I tried creating a Library/Application Support in my apps Xcode folder to no avail.
Specifically, I am making a game, and I want to include level packs in the game's Library/Application Support folder BEFORE I build and run the application. Preferably by dragging and dropping the files in Finder. Is this possible?
#Vimal Venugopalan
EDIT:
As Vimal mentioned, I could use [[NSBundle mainBundle] pathForResource:ofType:inDirectory:] method, but this gives a path similar to "~/MyApp.app/MyFolder/MyFile.plist". That is if "~" was the path to the app's home directory. Or more specifically "~" is the path returned by calling the NSHomeDirectory(); function. Where I want to put my files is in "~/Library/Application Support/MyFolder/MyFile.plist"
I want the files in this spot because I want to incorporate level-packs into my game. I want to include some level packs with the app download, and I would eventually like to have additional downloadable level-packs. Now the downloaded level packs definitely have to go in the "~/Library/Application Support/" folder (which I know how to do programmatically), so I would like to include the original level-packs in the same place before building and running the app. It would be so much simpler to have all my level-packs in one place!
You can add these files in the Project and access these files at runtime Xcode will copy them in the Copy Bundle Resource phase. This normally copies into the root of the bundle. To deal with directories see #CocoaFu's answer to this SO question.
Then in the code
NSBundle* bundle = [NSBundle mainBundle] will give you the main bundle
From this you look in directories using pathForResource:ofType:inDirectory: e.g.
NSString* path = [bundle pathForResource:#"file.xml"
ofType:nil
inDirectory:#"a"];
The methods are given in NSBundle class reference also see the Bundle Programming guide
Hope this solves your issue. If not please comment

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.

(iphone) How to store images in directory structure (vs flat documents directory) and related questions

I'm looking for a good way to manage many image files in my app.
When the number of images to maintain gets large, it seems vital to have a directory structure to hold them.(to organize images and to support duplicate image names)
Since normal way of storing images in documents directory(by adding images to xcode's resource folder) doesn't seem to support directory structure,
I probably need to use something called bundle.
Here are questions.
It's not clear to me what's the difference between "documents
directory" and "bundle". Are they
just names to specify directory path
inside an iphone application? Since
documents directory doesn't support
directory structure, I guess it's not
a regular file path in iOS? (I'm
looking for a definition or overview
description on those two terms enough
to help answering the following
questions)
How do I create a tree structure directory to store resources?
How do I add files to the created directory?
What is the step (probably in xcode) to add new files to the
directory as project grows? note:
question 3 deals with initial set up,
4 deals with update.
What happens to files under documents directory and bundle when
user updates the app? Since many
applications store user data
somewhere, there must be a way of
updating an app without wiping out the
saved user data. ie. How does
"documents directory" and "bundle"
behave in update situation?
So called "resource bundle" refers to the "bundle" used in above
questions?
Thank you.
Your app is a "bundle". It's represented by an NSBundle object. On iOS, this is the only bundle you are allowed to use; you can't create another bundle and import it into your app.
However, you can add a subdirectory to your app's bundle. In Xcode 4, select your project in the navigator and click on the Build Phases tab. Add a new build phase (bottom right), Copy Files. Move it up just below Copy Bundle Resources, and rename it something meaningful ("copy interface images", or something). You'll notice you've got a Subpath field there - that's your directory in your bundle. Drag the files you want in that subdirectory on to this build phase, and then you can access them through the normal methods in NSBundle, UIImage, NSData and so on.
Wish it was easier? Me too.