Load assets from bundle resources in Cocoapods

The problem

Yesterday I stumbled upon a problem when I loaded a xib file within my development pod.

I checked StackOverflow and found a few threads with the same issue:

I used the following code inside my Pod library to load a xib file which was also located in the same directory as my Pod source code.

When you add this Pod to your project to test, you’ll get a runtime error telling you that the nib can’t be found. It seems that the xib files you provide aren’t copied over.

How to load bundle resources in Cocoapods

So I checked my Podspec file and the CocoaPod spec reference guide, and saw you have an option to create resource_bundles.  All you need to do is add the assets you want to be added to the bundle.  They key you specify will be the name of the bundle.

So I changed my podspec file to copy all xibs in my Pod project

This code will add all the xib files inside the Pod directory (the ** will recursively check all subfolders of the Pod directory) to a bundle named MyCustomPod.

Having added this, it’s a good thing to run pod install again.

In your Pod code you can now add the following code to load the xib file from the bundle.