15

I am creating a static iOS framework (default template in Xcode 6) that includes xib files.

However I am having trouble loading the nib files when adding my framework to another app, I'm getting the following error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </private/var/mobile/Containers/Bundle/Application/89B7C8F1-698A-4E0B-AD8F-4DB414A6001B/Sample.app> (loaded)' with name 'MyNibName''

I have tried several solutions, some outlined here. I still could not resolve the issue.

I see a lot of references to this popular project which is now unsupported because xcode has the built in feature. Should I be using this instead?

In my framework target I have the xib files included in the 'Copy Bundle Resources' build phase and I see them inside the built framework directory, however, I noticed that the structure of the framework produced does not follow the structure outlined in apple's documentation. Even when I changed the framework structure (using versions and linking the Header and Resources folders to the current version) I still couldn't resolve the issue.

I tried loading my nib in the following ways:

1 loading the bundle by framework name, the bundle is nil in this case

NSString *resourceBundlePath = [[NSBundle mainBundle] pathForResource:@"myframework" ofType:@"framework"];
resourcesBundle = [NSBundle bundleWithPath:resourceBundlePath];
self = [super initWithNibName:@"MyNibName" bundle:resourcesBundle];

2 Creating a resource bundle and adding it to the framework. Also is nil. Note that this solution works if I add the bundle to the project separately, but that defeats the purpose of the framework.

NSString *resourceBundlePath = [[NSBundle mainBundle] pathForResource:@"resources" ofType:@"bundle"];
resourcesBundle = [NSBundle bundleWithPath:resourceBundlePath];
self = [super initWithNibName:@"MyNibName" bundle:resourcesBundle];

3 Loading bundle by class, still did not work, however it returns the same app bundle as in [NSBundle mainBundle]. The FrameworkClass is any class embedded in the framework.

NSBundle* bundle = [NSBundle bundleForClass:[FrameWorkClass class]];  

4 Loading the main bundle with [NSBundle mainBundle] which doesnt and shouldnt work.


I think what is happening is that the resources of the framework are not being included in the final app.

What am I missing? has anyone been able to use the default static framework template in xcode to make a framework with nib files?

Community
  • 1
  • 1
KDaker
  • 5,899
  • 5
  • 31
  • 44

3 Answers3

26

This should load your bundle.

Swift

let frameworkBundleID  = "com.framework.bundleId";
let bundle = Bundle(identifier: frameworkBundleID)

Objective C

NSString* const frameworkBundleID  = @"com.framework.bundleId";
NSBundle* bundle = [NSBundle bundleWithIdentifier:frameworkBundleID];
Srikanth
  • 1,861
  • 17
  • 29
  • perfect and precise answer , liked it great job – vishal dharankar Jul 09 '15 at 19:31
  • 2
    This worked for me, but does not fix the problem if a 3rdparty framework (static library) that you embed in your library is searching for `nib` resources supposed to be in the root folder of their part... – loretoparisi Feb 16 '16 at 16:40
4

this work for me:

import loginFramework/ClassA.h

NSBundle* resourcesBundle = [NSBundle bundleForClass:[ClassA class]];
ClassA * class = [[ClassA alloc]initWithNibName:@"ClassA" bundle:resourcesBundle];
UINavigationController * navigation = [[UINavigationController alloc] initWithRootViewController:class];

[self.window setRootViewController:navigation];

in app delegate in this case, and in the header of Framework add #import "ClassA.h"

if you want, I can send you an example

Lorenzo
  • 41
  • 2
0

SWIFT v5

I have created a variable in constant file

var frameworkBundle:Bundle? {
    let bundleId = "com.framework.bundleId"
    return Bundle(identifier: bundleId)
}

Then whenever I need bundle value I directly pass it like

collectionView.register(UINib(nibName: "MyCVCell", bundle: frameworkBundle), forCellWithReuseIdentifier: "MyCVCell")
Maneesh M
  • 193
  • 1
  • 10