AdamM
AdamM

Reputation: 4440

XCode accessing custom framework bundles resources i.e. images, xib files

I am creating a custom framework which contains many classes which I will be using in several different projects. I will be giving this library out to other developers so I need my code protected

I came across this tutorial which seems to be very useful

http://codefriend.blogspot.co.uk/2011/09/creating-ios-framework-with-xcode4.html

Using this, I have managed to turn my app into a framework, and all the classes are copied across and the code is protected as all the users will see is the .h files. So I can add this to any future apps, however I do not know how to access the resources, i.e images and XIB files.

One work around for images is to turn the .png files into a .png.h file, process is this

1) Open a terminal and "cd" to the directory that holds myImage.png
2) Run the command "xxd -i myImage.png myImage.png.h" -- This will turn the image into a .h header file
3) Include the new myImage.png.h file in the framework before building and make it a public header
4) After adding the framework to the project using it, import myImage.png.h
5) Load the data into a NSData object with [NSData dataWithBytes:myImage_png length:myImage_png_len];

This works fine for images, even if it is kind of messy way of doing it. However this does not seem to work for XIB files. As a test, in my framework project, I created a new UIViewController called TestController, then, I create the framework, add that to my new project, then try and access it like this

NSData *data = [NSData dataWithBytes:TestControllerVC_xib length:TestControllerVC_xib_len];
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    TestControllerVC *test = [[TestControllerVC alloc]initWithNibName:str bundle:nil];

    [self.navigationController pushViewController:test animated:YES];

The app crashes with this error

>     *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in
> bundle: 'NSBundle </Users/adam/Library/Application Support/iPhone
> Simulator/6.1/Applications/E9AEE10A-27A0-4363-B81D-D1DBE5DC7A51/TestAppWithFrameworkLib.app>
> (loaded)' with name <?xml version="1.0" encoding="UTF-8"?>.... (rest of xml printed)'

(It prints the XML of the TestController class, didn't want to clog up post with the whole xml file)

My question is, is there a better way of accessing the resources? If I access the TestBundle.Framework folder in my directory, you can see the resources file, but when you add the framework to your app, all you see is the header folder (see image below)

Is there a reason the resources folder isn't shown when you add the framework to your app? Is there anyway I can access the resources, as the xib file is definitely added to the resources as you can see in the image below. Any help would be much appreciated!!!

Figure

enter image description here

Upvotes: 1

Views: 1914

Answers (1)

AdamM
AdamM

Reputation: 4440

http://www.nearinfinity.com/blogs/tyler_vernon/2012/07/17/creating-and-using-static-libraries-for-iphone-using-xcode-4.3.html

For anyone else who requires creating a library and adding resources, this tutorial does the job for you. Time consuming to set up if you have a large amount of classes to put in your library, but does the job. Hope this helps anyone else having issues

Upvotes: 1

Related Questions