iPhone Application Support Folder Include Files BEFORE Building - iphone

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

Related

Seeing what files are in a folder in my application

I'm kind of new to ios developing and was wondering could I navigate through my own application with NSFileManager.
I have a folder inside my application and would like to see what files are in it and don't want to use hardcoded file names.
Sounds like you need to get to know NSFileManager's contentsOfDirectoryAtPath: error: method (documentation linked for you).
To look within your own application bundle, pass a path of "[[NSBundle mainBundle] resourcePath]". This would give you a path to the "Resources" folder within your application bundle. If you look at the linked documentation, you'll see NSBundle provides a few other potential ways to get paths to interesting locations within your application package.

Accessing files in Documents folder via jqtouch in iphone app

I was able to save some files in the Documents folder on my app. How can I access them on the phonegap side of things with jqtouch/javascript/html. I want to do something like:
$('#intro').attr('src','../../Documents/logo.png');
But, apparently, it's not letting me go up all those directories outside of www.
How can I resolve this?
There is a bundle file reader plugin, but it seems to read the contents, not reference it.
https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/BundleFileReader
Not sure you can do what you are asking about. Perhaps you could use the PhoneGap File API to save the files somewhere within your www dir instead? Then they would be available via html/js.

(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.

Move a folder to Documents directory

The iPhone application I'm working on comes bundled with 20MB of images in the application directory and I need to write new images to the documents directory over time.
Ideally I'd like to move the initial folder to the documents directory, copying the folder would mean wasting 20mb of the users disk space.
Failing that I'll create the required structure in the documents directory and leave the initial images in place, but that would mean checking in two places every time I want to display an image.
Is there a way of automagically searching the application bundle and documents directory when building an image path?
To answer your first concern: you can’t move anything out of the application bundle. Any modification of said bundle invalidates the signature and will render your app non-functional on all but jailbroken iOS devices.
Now, onto the challenge of copying images out: To find something in the main bundle, you’d do this:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"myImage"
ofType:#"png"];
For an image, though, you’re better off doing this:
UIImage *imageFromBundle = [UIImage imageNamed:#"MyImage.png"];
That will do caching for you in a way that loading the image directly won’t.
I don’t know why you’d want to copy the image out, but it seems that maybe you have an initial set of images that may or may not be replaced. I’d recommend, then, instead of copying out of the bundle, look for the image first in the Documents folder, and if it isn’t there, copy it out of the bundle. That allows you to replace the images without copying from the bundle or modifying the bundle.
AFAIK the bundle is read only. So while your "move" idea is a good one, I am fairly certain Apple does not allow for it currently.
Is there a way of automagically
searching the application bundle and
documents directory when building an
image path?
I can say that I have done this for images but you have to create the magic yourself. I wrote a class that I would call with the name of the image as a parameter. That class would first look in the documents directory, then look in the bundle and finally look to a website for the image. When it found the image it returned the UIImage. A bit of a pain to initially setup, but once in place it is quite useful and I am glad I did it.

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

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"]