2

I'm taking over a project I did not start and I'm still a junior, so I need some help to clarify my mind. I'm developing a library for iOS 7.0 and up. This library is then distributed trough cocoapods.

I've build a custom view and how I have a .xib file containing it. It works perfectly in my dev app (because I'm using directly the code), but when I test it in my staging app (which loads the library trough a local pod) I get

NSInternalInconsistencyException Could not load NIB in bundle: 'NSBundle </Users/Jack/Library/Developer/CoreSimulator/Devices/899B54FC-8A37-4182-9D89-109251B58555/data/Containers/Bundle/Application/36A3B225-AEF2-42B7-8AEC-5D930F4C4BCA/StagingApp.app> (loaded)' with name 'CheckBoxWithText''

The .xib file is in the copy file build phase and is part of the target I'm using to build the library.

I'm loading the xib like this

[[NSBundle mainBundle] loadNibNamed:@"CheckBoxWithText" owner:self options:nil];

But it crashes as soon as the components gets loaded on screen.
Some people were saying to use the bundle of your library, but I don't think my library has an identifier ( can't see it on info).
I tried using

[[NSBundle bundleWithIdentifier:@"com.company.project"] loadNibNamed:@"CheckBoxWithText" owner:self options:nil];

but it does not load anything.

I've spent the afternoon reading possible solution but now I've a lot of confusion in my head.

A static library (like mine) is not supposed to contain resources like xib, yet I'm packing some images with it. Am I missing something here?
I've read a tutorial about a guy who achieved this using a budle target, but I haven't really understood how he did it and how would I use that target given that I'm supposed to use my aggregate target to build the library.

A dynamic framework would solve the problem if not for the fact that I can't use it on iOS7.0.

Is my only option to build the layout programmatically?
Or am I just missing something and it's possible to pack the .xib file?

Jack
  • 57
  • 6

1 Answers1

0

CocoaPods doesn't seem to pick up .xib files when you specify them using resources in your .podspec. Using resource_bundles instead works fine.

So the .podspec would read:

s.resource_bundles = { 'CheckBoxWithText' => ['CheckBoxWithText.xib'] }

The Nib would be loaded like this:

NSBundle* bundle = [NSBundle bundleForURL:[NSBundle.mainBundle URLForResource:@"CheckBoxWithText"
        withExtension:@"bundle"]];
NSArray* nib = [bundle loadNibNamed:@"USACheckBoxWithText" owner:self options:nil];

Now there is another bug in CocoaPods which prevents asset catalogs to be loaded from resource_bundles. You should avoid asset catalogs for now.

Note that both bugs would be solved if you use iOS 8 as minimum and develop your Pod in Swift.

fluidsonic
  • 4,655
  • 2
  • 24
  • 34
  • I've tried `[[NSBundle bundleForClass: FeedbackController.class] loadNibNamed:@"CheckBoxWithText" owner:self options:nil];` as well as `[[NSBundle bundleForClass: [FeedbackController class]] loadNibNamed:@"CheckBoxWithText" owner:self options:nil];` but the error is still there – Jack Dec 08 '15 at 16:52
  • How did you add the `.xib` file to your CocoaPod? Using [resources](https://guides.cocoapods.org/syntax/podspec.html#resources) or [resource_bundles](https://guides.cocoapods.org/syntax/podspec.html#resource_bundles)? – fluidsonic Dec 08 '15 at 16:57
  • I wasn't really sure, so I went for both: `s.resource = "ubForm/Images.xcassets/**/*.*" s.resource = "ubForm/**/*.xib" s.resource_bundles = "ubForm/**/*.xib"` – Jack Dec 08 '15 at 17:08
  • There are multiple issues. First you should only use one of the approaches not both. Let's opt for `resources`. Second you should specify the asset catalog itself, not it's children. `s.resources = ['ubForm/Images.xcassets', 'ubForm/**/*.xib']` - Next thing would be that you check the folder `Pods/CheckBoxWithText` in the project which uses your Pod. Find the Nib file in there and check how it's called and where it is located. Don't forget to perform a `pod update` first after changing your podspec. – fluidsonic Dec 09 '15 at 09:25
  • I've done what you said and updated my podspec. Looking in the pod folder of my staging app I can see the resource folder with all the images, but I can not see any .xib file. I've tried various way of specifying the resource folder, just to be sure. If my podspec was being ignored I should not have the images catalog as well, so it's not that I think. – Jack Dec 09 '15 at 10:05
  • I just set up a test project and there seems to be a limitation that you cannot use `.xib`s with `resources`. It only works with `resource_bundles`. Will update my answer. – fluidsonic Dec 09 '15 at 10:31