How to deploy a compiled iOS static library that uses other libraries

I recently ran into an issue that had me stumped for quite some time and wanted to capture the solution here both to help anyone in a similar situation and for future reference, or if someone wants to provide a comment to let me know I’m doing it all wrong and point me in the right direction!

What I wanted to achieve was to create a compiled static library that could then be published to cocoapods for easy use now there are plenty of resources that describe how to create a static library that uses other libraries and deploy it as source, one of the better ones I was using was at sigmapoint however none describe how to deploy as a compiled library, hence why I’m writing this post.

After working my way through the posts above I had my library with a Podfile, that had my dependency:

pod 'AFNetworking', '~> 2.2'

And had set my MyLib.podspec to have the listed dependency:

s.dependency 'AFNetworking', '~> 2.2'

Then in my app I added MyLib to the Podfile:

pod 'MyLib', :path => '../pods'

And then came across around 270 duplicate symbols errors, sigh.

My reasoning as to why this was happening is that in the MyLib cocoapods is compiling the AFNetworking code into libPods.a and this is being used to produce MyLib.a, then when the app is being compiled it too also has the reference to AFNetworking and is also trying to compile it into libPods.a however as it is alread in MyLib.a it runs into duplicate symbols errors.

So in order to solve this I did the following:

  1. Changed MyLib so it did not use a Podfile and instead just brought down just the header files for AFNetworking for the version I’m targeting and added them to the search path config
  2. Removed binary link to libPods.a from MyLib
  3. Did a pod update against my app and hey presto it build successfully!

What this does is allow MyLib to compile using the header files of AFNetworking but to not compile any of the concrete AFNetworking code into MyLib and instead AFNetworking only gets compiled in at the app compilation stage.