How to transfer resource files with Static Library (How to wrap resources in bundle)?

Midhun MP picture Midhun MP · Apr 30, 2013 · Viewed 8.4k times · Source


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.

Answer

Nishant Tyagi picture Nishant Tyagi · Apr 30, 2013

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.