iOS Modularization - How to use your resources bundle in CocoaPods library

Thân Đặng
2 min readJun 8, 2020

Some of us have had changes to write a module which working independently with the app. In terms of this point, I’ll talk about UI module where we have two options:

  • Rewrite as IBDesignable with manually coding
  • Or using xib file where we could outlet and keep everything simple as we have in the app. Or even image bundle.

For those who work on option 1, we have full control without other dependencies setup. I’m going on second one where sometimes we miss on getting bundle resources to use from target app with from bundle selected.

Let say my library name is: MyCustomView. Our *.podspec file will be like this:

Pod::Spec.new do |s|

s.name = ‘MyCustomView’

s.version = ‘1.0’

s.summary = ‘A short description of MyCustomView.’

s.homepage = ‘https://github.com/thandang/mycustomview'

s.license = { :type => ‘MIT’, :file => ‘LICENSE’ }

s.author = { ‘Than Dang’ => ‘than.dang@groupmail.com’ }

s.source = { :git => ‘git@github.com:thandang/mycustomview.git’, :tag => s.version.to_s }

s.ios.deployment_target = ‘10.0’

s.source_files = ‘MyCustomView/Classes/**/*’

s.resource_bundles = {

‘MyCustomView’ => [‘MyCustomView/Assets/*.{xcassets}’, ‘MyCustomView/Assets/*.xib’]

}

s.resources = [‘MyCustomView/Assets/*.{xcassets}’, ‘MyCustomView/Assets/*.xib’]

We usually set s.resource_bundles only where we missed s.resources. And this will cause the issue that your bundle resources from CocoaPods library can not be access from outside due to wrong target. Bellow is an example where I load cell as xib file and its images bundle.

Error upon wrong setup on bundle resources.

So, it should be target on your Library as well as target project, which is temporary and causing crash on project target.

Correct should be in the right one where you setup correct in *podspec file.

Just to keep in mind that putting one more line to specify your resources bundle where it will be referred from.

Hope this helps.

--

--