How to make an iOS asset bundle? How to make an iOS asset bundle? ios ios

How to make an iOS asset bundle?


Answer is stupidly simple

Make a folder in finder, add files to it, rename it to bundlename.bundle

drag into Xcode - success!

to access, use the form of PathToMainBundle+"/bundlename.bundle"


How to create a bundle

  1. Create a folder in finder.
  2. Add files to the folder
  3. Rename the folder so that its extension is .bundle (e.g. "New folder" -> "BundleName.bundle")

PS: You can at any time right click the folder and hit "Show package content" in order to add, remove or modify any of the files.

How to add the bundle to Xcode

  1. Drag it into Xcode

How to use the bundle

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"BundleName" ofType:@"bundle"];NSBundle *bundle = [NSBundle bundleWithPath:bundlePath]; NSString *resource = [bundle pathForResource:@"fileName" ofType:@"fileType"];

(Replace BundleName, fileName and fileType with appropriate names)


Two other helpful bits of advice:

First, in order to see the contents of the bundle in Xcode you need to set its type in the File Inspector Utility Pane, to "Application Bundle". You still won't be able to copy to and from via Xcode. You'll need to use Terminal but Xcode will update it immediately.

Second, in order to use resources in the bundle here's a helpful snippet...

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"AquarianHarp" ofType:@"bundle"];NSString *imageName = [[NSBundle bundleWithPath:bundlePath] pathForResource:@"trebleclef2" ofType:@"png"];UIImage *myImage = [[UIImage alloc] initWithContentsOfFile:imageName];

As mentioned in my comment above, you needn't actually load the bundle (you can't as it's not executable) and the ofType needs to match the case of your actual file for it to work on the device. It will work either way in simulator so don't be fooled by this red herring!

Finally, you don't need to put your resources in the "Resources" subfolder inside the bundle. It seems you can use an arbitrary layout but there may be unknown performance implications.