Cocoapods: Using XIB from Cocoapods in main project Storyboard won't load (resources are not copied when source_files are specified)

Created on 23 Jun 2016  Â·  17Comments  Â·  Source: CocoaPods/CocoaPods

Related: https://github.com/CocoaPods/CocoaPods/issues/5105

Report

Cocoapods: 1.0.1
The issue I am encoutering is described in the following stackoverflow thread: http://stackoverflow.com/questions/36514072/load-xib-from-cocoapod-in-storyboard
I have a problem where I cannot load the XIBs from my Cocoapod. The problem is resolved if I manually edit the Pods-defaults-MyApp-resources.sh and add the necessary .xib files under the install_resource directive.

What did you do?

The attached project demonstrates the problem.
Archive.zip

The storyboard has a segue to another view controller, which is set to a class that
has a XIB in the Cocoapod (I am using PMAlertController for this example, since it has a XIB).
screen shot 2016-06-23 at 09 53 48
The view controller will not load through the storyboard, but will load in code, when a bundle is explicitly specified. Both are demonstrated in the sample project.
The problem is resolved if the XIB is copied into the project folder, which moves it into the default bundle.

What did you expect to happen?

I expected the files under "resource_bundles" and "resources" to be copied in the same way as if no "source" files are specified. This way I would be able to use those resources in the main project, such as XIBs and images without explicitly specifying the bundle.

What happened instead?

The XIB specified in the storyboard only loads if the bundle for it is explicitly specified in code, which cannot be done in the storyboard, so it doesn't load.
This effectively renders storyboards unusable in the case where the external XIB files have to come from Cocoapods.

Additional notes

Is there a way we can have a functionality of copying our resources, despite providing source files?
Specifying resource_bundles with the same name as the Pod seems to create another target in the Pods project:
screen shot 2016-06-22 at 16 28 44
and doesn't work for the purpose. Please advise.
It would be great to have the ability to copy resources, as described in https://github.com/CocoaPods/CocoaPods/issues/5105 regardless of source directive being present or not.

awaiting input

Most helpful comment

Bump

I am getting this as well. Whenever I try and include a .bundle of images, or .xcassets of images it get's targeted to a new target PodName-PodName (and toolbox is a grey plugin). The only way to access the images within the Pod is the manually set the target back to the main pod framework target. If I remove the resources_bundle it doesn't create the second target, but then my images are not included. All variations of trying to find the files using bundle paths... etc fail unless the main target is explicitly set on the images.

secondcptarget

using Swift 3.0 on Xcode 8

All 17 comments

Don't think this is a CocoaPods issue, the problem likely lies in PMAlertController using resource_bundle, therefore leading to creating a nested bundle inside the framework bundle, a setup which IB probably does not understand.

@neonichu It's not. I have used this with several cocoapods and each time I got the issue regardless whether it's in "resources" or "resource_bundles", including several of our own private cocoapods.

Do you have an example where resources is used and loading the NIB doesn't work as expected?

I'll try to create a sample project today. Though, isn't "resources" now deprecated in favor of "resource_bundles"?

It is, but it's still supported, it's just not as useful now people are using frameworks.

I updated the original project, adding a new development pod, which uses "resources"
Archive.zip

Thanks for the sample, I wonder if this is a setup that IB even supports? Intuitively, I would have expected that specifying a module for loading a VC's custom class would also ensure that the NIB is loaded from that same bundle, but that appears to not be the case. Just throwing all NIBs in the main bundle does not feel like the right answer to this.

I have commented above - if the files in the "resources" or "resources bundles" are copied the same way as the resources are when no "source" files are present - then it works perfectly. It is definitely a cocoapod issue, because if I manually add the XIB files to the copy resources script, then the problem is resolved.

Do you want me to provide instructions which modifications need to be done to the shell script so that XIBs from Cocoapods work?

Thanks @doubov, I think you have given us all the information.

We still need to dig around a bit to confirm that IB really doesn't support anything but the main bundle — this seems really weird to me.

Bump

I am getting this as well. Whenever I try and include a .bundle of images, or .xcassets of images it get's targeted to a new target PodName-PodName (and toolbox is a grey plugin). The only way to access the images within the Pod is the manually set the target back to the main pod framework target. If I remove the resources_bundle it doesn't create the second target, but then my images are not included. All variations of trying to find the files using bundle paths... etc fail unless the main target is explicitly set on the images.

secondcptarget

using Swift 3.0 on Xcode 8

This issue will be auto-closed because there hasn't been any activity for a few months. Feel free to open a new one if you still experience this problem :+1:

I'm facing this issue now - has anyone found a solution?

Can this be reopened?
when using resource/resources it should be copied to the main bundle, but it's not doing it when you also have source_files, when source_files is present then it adds it to the framework bundle instead

I can't tell if my situation is related. I have a development pod that has both source_files and resource_bundles. The code in the Framework references XIBs in the bundle. The main app project includes this pod. The generated Pods-App-resources.sh never calls install_resource. I can fix this by adding the line:

install_resource "$BUILT_PRODUCTS_DIR/LibUI/LibUIBundle.bundle"

I also had to manually refer to the bundle in the Dev Pod's code, like:

let curBundle = Bundle(for: MessageView.self)
let resourceBundleURL = savoryUIBundle.resourceURL?.appendingPathComponent("LibUIBundle.bundle")
let resourceBundle = Bundle(url: resourceBundleURL!)
let messageView: MessageView = UINib(nibName: "MessageView", bundle: resourceBundle).instantiate(withOwner: nil, options: nil)[0]

Additionally, all of the App's dependencies have Mach-O Type set to Dynamic Library by default, and I had to change them all to Static Library.

I also had to go through a similar manual process for GoogleMaps. Manually doing "Add Other..." for the Frameworks in the "Link Binary with Libraries" build phase (there's no GoogleMaps target in the Pods project) and an install_resource "${PODS_ROOT}... line for the bundle embedded in GoogleMaps.Framework. Then I was able to reference GMSMapView within my Storyboard.

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.

This issue will be auto-closed because there hasn't been any activity for a few months. Feel free to open a new one if you still experience this problem :+1:

Was this page helpful?
0 / 5 - 0 ratings