12

I am having some difficulty creating a bundle for my application, and placing files in the bundle.

For example, Facebook has developed a bundle for iPhone applications using their framework. In the same way, I also want to create a bundle which can be reused for many applications.

My questions are:

  • what steps should I follow to create a bundle for any kind of application?
  • what should be taken care while creating a bundle?
halfer
  • 19,824
  • 17
  • 99
  • 186
sagarkothari
  • 24,520
  • 50
  • 165
  • 235

2 Answers2

13

First of all, since your question is tagged iPhone, you can only include code in your bundles on the iPhone. So basically you can only use bundles to package up pictures and sound files and other static data.

When you create a new project in XCode, there is an option to make the target a bundle (under Framework & Library), but an assets bundle is just a directory with a .bundle suffix. I generate mine with this little script:

#!/bin/bash
echo "Building assets bundle."
if [ -d ./MyAssets.bundle ]; then
   rm ./MyAssets.bundle/*
else
   mkdir ./MyAssets.bundle
fi
find ./assets -type f -print0 | xargs -0 -J% cp % ./MyAssets.bundle

(I'm no bash hacker, so this can probably be improved in countless ways. Suggestions welcome!)

This takes a folder hierarchy and flattens it (I detest hierarchies) into a single directory which is named MyAssets.bundle. I trigger this script from a separate build phase when in projects that import the bundle, so that changes are automatically tracked.

If you want to learn how to create framework bundles, it's a bit more complicated (you have to follow certain conventions and include info in plists), but for iPhone bundles, this is pretty much all you will need to know and do.

Felixyz
  • 19,053
  • 14
  • 65
  • 60
  • Is it possible to load xib files from this bundle, because I haven't found a way to do that? – vakio Sep 21 '10 at 12:48
  • You can use the appropriate bundle in both [[NSBundle mainBundle] loadNibNamed:@"MyViewController" owner:self options:nil] and [aViewController initWithNibName:@"MyViewController" bundle:nil] (nil just means the main bundle) – Felixyz Sep 23 '10 at 06:08
  • But you can't have an external bundle, right? Like NSBundle *bundle = someFunctionToLoadExternalBundle(); [bundle loadNibNamed:@"name" owner:self options:nil]; I have googled and only found that it can't be done. Just checking if you discovered a miracle way to do it or something, but I know it's probably never going to happen. – vakio Sep 23 '10 at 16:29
  • 1
    What do you mean by external bundle? You of course need to include the bundle in your app bundle. Then you can use it like this for example: UIImage* myImage = [UIImage imageNamed:@"MyBundle.bundle/images/myImage.png"]; – Felixyz Sep 23 '10 at 18:34
  • And in general, rather than just googling, I'd recommend taking the time to read Apple's extensive documentation on topics like this: http://developer.apple.com/library/ios/#documentation/corefoundation/Conceptual/CFBundles/ – Felixyz Sep 23 '10 at 18:42
  • I'm talking about loading xib files from a bundle other than the main bundle, via loadNibNamed:owner:options or initWithNibName:bundle:, which is from my understanding an impossibility on the iPhone. – vakio Sep 27 '10 at 08:09
  • I haven't done it myself, but I very much doubt that there would be a method called initWithNibName:bundle: if you could only load nibs from the main bundle. But it would be better if you posted a new question instead of continuing on this commentary thread. – Felixyz Sep 27 '10 at 17:17
8

You could do this too:

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"

Source: https://stackoverflow.com/a/5277452/736384

Community
  • 1
  • 1
Luis Ascorbe
  • 1,985
  • 1
  • 22
  • 36