3

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!

mogelbuster
  • 1,066
  • 9
  • 19
  • Hi modelbuster, did you get an answer to this question? I am able to put a full folder into the app bundle as per the link to @CocoaFu's answer below, and access my folder and files fine through the bundle. However, I still have not figured how to to put the files into the Application Support folder and what is a good way to access the files. – kev Mar 25 '14 at 10:12

1 Answers1

3

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

Community
  • 1
  • 1
Vimal Venugopalan
  • 4,091
  • 3
  • 16
  • 25
  • This is great help, thank you! The only thing I was missing was the "Create folder references for any added folders" radio button. – mogelbuster Sep 20 '12 at 03:04
  • This is slightly different than what I am looking for. The difference is this: Let's designate "~" as the app's home directory. The folder referencing and pathForResource method you mentioned will give you a path to something like this: "~/MyApp.app/MyFolder/MyFile.plist" Where I want to include my file is in "~/Library/Application Support/MyFile.plist". Your solution could work, but read my edited original post as to why I want this particular folder. Thank you again! – mogelbuster Sep 20 '12 at 03:14
  • Unfortunately there is no way to automatically do what you want. The best you can do is do as Vimal suggested and include the files in your resource bundle. Then when your app starts the first time copy the files from the bundle to the Application Support directory. – honus Feb 22 '13 at 04:24