10

Possible Duplicate:
Can you reference Xib files from static libraries on the iPhone?

I have created an iOS framework and it includes a xib view. When I debugged the program, this line of code worked fine:

MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyView" bundle:[NSBundle mainBundle]];

However, when I 'reference' the framework from another project - the nib is no longer in the 'mainBundle'. What should I do with the code above (that is part of the framework) so it loads from the framework and not the consuming application project?

Community
  • 1
  • 1
ConfusedNoob
  • 9,826
  • 14
  • 64
  • 85
  • Sorta related [Can you reference Xib files from static libraries on the iPhone?](http://stackoverflow.com/questions/707429/can-you-reference-xib-files-from-static-libraries-on-the-iphone) – CodaFi Sep 24 '12 at 02:10
  • @ConfusedNoob - is this a static lib or a [universal framework](https://github.com/kstenerud/iOS-Universal-Framework) for iOS? – FluffulousChimp Sep 24 '12 at 02:21
  • It's a universal framework as per this post - http://spin.atomicobject.com/2011/12/13/building-a-universal-framework-for-ios/ but it starts life as a static library. – ConfusedNoob Sep 24 '12 at 02:40

1 Answers1

19

The framework in question should have a bundle identifier in its Info.plist file typically. In the application that makes use of this custom framework, you should be able to access resources in this bundle by:

NSString *frameworkBundleID = @"com.yourCompany.YourFrameworkBundleID";
NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:frameworkBundleID];

This is the NSBundle from which you can access framework resources.

EDIT:

There appears to be an alternate (possibly better) means of accessing the bundle in question.

e.g.:

NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"MyLibraryResources" withExtension:@"bundle"]];

See this excellent tutorial

FluffulousChimp
  • 9,157
  • 3
  • 35
  • 42
  • I'll try that. Since the code is inside the framework, is there a more 'introspective' way of doing this instead of having the name encoded in the framework... e.g. [NSBundle currentBundle]; // silly pseudocode – ConfusedNoob Sep 24 '12 at 02:28
  • 1
    downvote - really? the OP stated "iOS framework" by which one might reasonably assume something like [this](https://github.com/kstenerud/iOS-Universal-Framework) in which resources **can** be packaged. – FluffulousChimp Sep 24 '12 at 02:30
  • @ConfusedNoob unsure about introspecting you suggest. – FluffulousChimp Sep 24 '12 at 02:33
  • How do I find out the name of my bundle in a framework? It isn't on the Info tab of the project as it started life as a cocoa static lib? – ConfusedNoob Sep 24 '12 at 18:45
  • The tutorial link still requires "Entire" framework project to be imported inside client app. What's the use? It doesn't obscure the implementation at all...useless link. – Nirav Bhatt Jun 05 '15 at 07:46
  • I don't know bout the second part after the Edit, but the first part was very helpful. Thanks! – Dan Rosenstark Feb 02 '16 at 00:33
  • Agreed the second part is no good—no idea why it would seem better to anyone. Not only is it just more complicated it's also more brittle and dependent on implementation detail. – Christopher Swasey Jan 20 '17 at 15:24