Cocoapods unwanted files

I'm really a fan of Cocoapods and I'm learning more and more about them every time we use them in one of our projects (just like in last post). This time I have confirmation that there's no easy little command you can use to deal with this problem.

While making one of the projects I ran into a problem - I wanted to use two Pods - AFNetworking and SDWebImage. One is great for network communication, the other for loading images asynchronously from URL. Unfortunately both of those libraries have categories added to UIImageView with the same method names. I wanted to use SDWebImage methods because they did give me a bit more control over how those images are loaded and cached AFNetworking classes was used in several places as well. You can't choose or exclude selected files from Pod - only person creating Pod can do that.

Solutions? A few at least. The one which is the best in the long run and suggested by Eloy DurĂ¡n is to fork both libraries on github, prefix those methods in both libraries (as you should when you're creating categories which are then used in more then one project) and make pull request. It works, shows the power of OSS and will help other people as well. And I will end up doing that, once I will find a bit of time to make it right. In the mean time, while waiting for pull request to be accepted, you can use your own fork in Podfile by using :git option:

pod 'AFNetworking', :git => 'github.com/YOURUSERNAME/AFNetworking'

But of course since I like to try different things and it's an interesting way of learning Ruby I found another way - by using post_install hook which I also used last time. This time instead of changing configuration for Pods target I modified files which I didn't want to use in my project.

post_install do |installer_representation|
  installer_representation.pods.each do |pod_representation|
    pod_representation.source_files.grep(/AFNetworking\\/UIImageView/) { |element| File.open(element, 'w').write("/* Removed due to conflicting method names */") } 
  end
end

This way every time anyone will use pod install on that project all Pods will be installed as always but files with UIImageView category will be overwritten with comment which says why they are empty. Of course you could try to delete those files entirely but it messes up with the project which you would have to fix (by hand or by another script) and may cause problems if those files are imported/included in some other ones (like one big main header file which for convenience just imports all classes from the library).

Posted on
Tagged in tips , cocoapods
Jarek Pendowski

Want to talk?

comments powered by Disqus