8


I'm creating a Static Library for iOS applications. I almost completed it, but there is an issue with the resources.

My Static Library uses a lot of images and sound files. How can I transfer it with my Static Library ?

I know we can wrap it on a bundle and give it along the .a file. But I don't know how to wrap Images and sound files in a Bundle file.

What I did:

  • I searched a lot, but couldn't find any useful links.
  • I got Conceptual CFBundles reference, but didn't find any solution for my problem.
  • I checked the file templates available for XCode, but didn't saw any bundle type other than Settings Bundle.
Midhun MP
  • 103,496
  • 31
  • 153
  • 200

1 Answers1

9

There are several good reasons to build an application with multiple bundles and several different ways to do it. To my experience the best way is open Xcode and create a new bundle project:

  1. Select: File -> New Project… -> Group Mac OSX (!) -> Framework & Library -> Bundle. Add your resource files to the project.
  2. Build the bundle as you build other iPhone apps.
  3. You may add this project to your static library project and rebuild it all the time the library is changed. You must know that the bundle itself will not be linked to your library file.
  4. In your app projects add the .bundle file to your project as an ordinary resource file (Add -> Existing Files… -> find and select the above built .bundle file. Do not copy it).

Example :

// Static library code:
#define MYBUNDLE_NAME       @"MyResources.bundle"   
#define MYBUNDLE_IDENTIFIER @"eu.oaktree-ce.MyResources"
#define MYBUNDLE_PATH       [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]
#define MYBUNDLE            [NSBundle bundleWithPath: MYBUNDLE_PATH]

// Get an image file from your "static library" bundle:

- (NSString *) getMyBundlePathFor: (NSString *) filename
{
        NSBundle *libBundle = MYBUNDLE;
        if( libBundle && filename ){
            return [[libBundle resourcePath] stringByAppendingPathComponent: filename];
        }
        return nil;
}

// .....
// Get an image file named info.png from the custom bundle
UIImage *imageFromMyBundle = [UIImage imageWithContentsOfFile: [self getMyBundlePathFor: @"info.png"] ];

For more help you can check these good articles

  1. iOS Library With Resources

  2. Resource Bundles

Hope it helps you.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
  • one thing: imageWithContentsOfFile is poor in case of performance it is better to use imageNamed as it is using caching so it can be faster :) additionally you can achieve same result with less code by simple: `[UIImage imageNamed:[NSString stringWithFormat:@"my.bundle/%@",imageName]];` and simply put the right bundle name – Julian Apr 01 '14 at 13:44
  • there will be 2 things needed to integrate such library, .a file and .bundle file, right? which user has to import separately manually? – BaSha Apr 21 '14 at 13:08