4

I get a crash as soon as I try to show a view from a bundle. Here's the setup:

  • Project contains a bundle CFramework.bundle inside the app's main bundle
  • CFramework.bundle contains GigyaFBPreviewController.xib and images it uses, in its root
  • GigyaFBPreviewController.m is in a static library referenced by the project

Code:

NSString* bundlePath = [[NSBundle mainBundle] pathForResource:@"CBCFramework" ofType:@"bundle"];
NSBundle* bundle = [NSBundle bundleWithPath:bundlePath];
GigyaFBPreviewController* gigya = [[GigyaFBPreviewController alloc] initWithNibName:@"GigyaFBPreviewController" bundle:bundle];
[self presentModalViewController:gigya animated:YES];

The code is executed after a button click, and crashes on the last line. GigyaFBPreviewController is just a UIViewController, and it uses the default initWithNibName:bundle:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 
'Could not load NIB in bundle: 'NSBundle </Users/mjovanovic/Library/Application Support/iPhone Simulator/4.3.2/Applications/A40F8D71-EB88-4EB5-B9D3-CFD330C57F24/socialmediatest.app/CBCFramework.bundle> (not yet loaded)' with name 'GigyaFBPreviewController''
*** Call stack at first throw:
(
    0   CoreFoundation                      0x01ad85a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01c2c313 objc_exception_throw + 44
    2   CoreFoundation                      0x01a90ef8 +[NSException raise:format:arguments:] + 136
    3   CoreFoundation                      0x01a90e6a +[NSException raise:format:] + 58
    4   UIKit                               0x00e050fa -[UINib instantiateWithOwner:options:] + 2024
    5   UIKit                               0x00e06ab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
    6   UIKit                               0x00cbc628 -[UIViewController _loadViewFromNibNamed:bundle:] + 70
    7   UIKit                               0x00cba134 -[UIViewController loadView] + 120
    8   UIKit                               0x00cba00e -[UIViewController view] + 56
    9   UIKit                               0x00cbba3d -[UIViewController viewControllerForRotation] + 63
    10  UIKit                               0x00cb7988 -[UIViewController _visibleView] + 90
    11  UIKit                               0x00f5993c -[UIClientRotationContext initWithClient:toOrientation:duration:andWindow:] + 354
    12  UIKit                               0x00c3181e -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 954
    13  UIKit                               0x00eb9619 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 1381
    14  UIKit                               0x00cbe65d -[UIViewController presentModalViewController:withTransition:] + 3478
    15  socialmediatest                     0x000027bb -[socialmediatestViewController clicky:] + 283
    etc

Relevant info:

  • If I take the nib file out of CFramework.bundle and put it into the project, it works fine. However, I need it in a bundle to distribute with a static library.
  • The bundle exists inside the .app and calling [bundle pathForResource:@"GigyaFBPreviewController" ofType:@"xib"] returns the correct path.
  • If I remove the image reference from the nib, nothing changes. All images are referenced as CFramework\image.png
  • The "(not yet loaded)" error message is really weird. I found a bunch of posts with people running into the same exception when switching to Xcode4, but their solutions didn't work for me.

* SOLUTION *

The xib was not compiled into a nib and it could not be loaded, duh. Thank you Joshua Weinberg for providing the link below in the comments!

meelawsh
  • 617
  • 6
  • 8

2 Answers2

2

First of all if a bundle is not created properly it will not get loaded. So in-order to create a proper bundle below are the steps for creating a bundle:

  1. Add a new target by choosing a templete named bundle under OS X -> Framework & Libraries.

  2. Select newly created target and change BaseSDK from OSX to Latest iOS.

  3. Add .xibs, images or other resources which you want to use it from bundle in Build Phrases -> Copy Bundle Resources.

  4. Add CoreFoundation framework in Build Phrases -> Link binary with Libraries.

  5. Compile the target choosing iOS Device.

  6. Save the newly created bundle from Products directory to some place.

Now copy that bundle into your main project. Load a bundle using below code:

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

NSBundle *bundle = [NSBundle bundleWithPath:path];

You are now set with the new bundle.

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
Dharmesh Siddhpura
  • 1,610
  • 12
  • 22
0

You can also get this error if you don't have your target dependencies setup correctly and Xcode parallelises your build - i.e. it will build multiple targets simultaneously.

I had an error where Project-A built sub Project-B and included Project-B-Resources bundle. Project-B built sub Project-C, which built it's own Project-C-Resources. Project-C resources were bundled inside Project-B resources. In Xcode > Build Phases > Target Dependencies - if I put Project-B-Resources above Project-B, then the bundle would be packaged up incorrectly. I needed to ensure Project-B was listed first, to ensure it triggered the Project-C-Resources build correctly.....

... bit of a mouthful and to get your head around the above, but it's something to explore if you're having problems with a missing nib inside nested projects

https://developer.apple.com/library/ios/recipes/xcode_help-scheme_editor/Articles/SchemeBuild.html

bandejapaisa
  • 26,576
  • 13
  • 94
  • 112