error when using a bundle in an iOS project / how

2019-07-18 20:47发布

问题:

I'm trying to include a static library in an iOS project. I imported the whole static library project then I linked the lib in the 'target dependencies' and 'link binary with libraries' as that's explained here: http://www.applausible.com/blog/?p=657

But then I'm getting this error when testing the app :

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/alexis/Library/Application Support/iPhone Simulator/5.0/Applications/9811107D-0D7E-4178-ACCE-BF43E3043770/PlazappPartnerTest.app> (loaded)' with name 'LauncherView''
*** First throw call stack:
(0x188a052 ......

I guess it can't access the .xib file but since it's included in my library I don't know why. How do I make the main project 'see' the .xib file? (If that's actually the problem...)

EDIT:

I tried to make a bundle containing the .xib files and I included it to my main project but I can't use this bundle. I tried this :

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"BundleName" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];

NSError *error;
[bundle loadAndReturnError:&error];

NSLog(@"bundle error : %@", [error userInfo]);

And I got this error :

NSLocalizedDescription = "The bundle \U201cBundleName.bundle\U201d couldn\U2019t be loaded because its executable couldn\U2019t be located.";
NSLocalizedFailureReason = "The bundle\U2019s executable couldn\U2019t be located.";
NSLocalizedRecoverySuggestion = "Try reinstalling the bundle.";

What's the problem?

I think I can't load a bundle containing non-executables resources like .xib files. Am I right?

But then, how can load my .xib from the bundle?

回答1:

Frequently in Cocoa programming there's more than one way to get a bundle. I've had success with this:

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

Then load the nib with this:

MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:libBundle];

This specific call you make to loadAndReturnError: loads the bundle's executable code. As this bundle probably doesn't have any executable code, just the collection of resources the library needs, it will give you this error.

Edit (Responding to a comment):

I made my bundle by adding a new target to my library project for the bundle. Starting with the Mac OS X bundle template, I added all my image, .xib, and other resources to the "Copy Bundle Resources" build phase. I changed the base SDK to iOS (latest). Then I added the bundle to the library's dependencies and final app's "Copy Bundle Resources" build phase.

If you just try the add .xib files to a folder method, the .xibs are not compiled into nibs, and iOS can't read them.