Develop on iOS? You Must Use Cocoapods.
Reduce the need to manage complex dependency organization while using XCode. Cocoapods allow you to integrate external code or projects into your iOS project.
Join the DZone community and get the full member experience.Join For Free
Okay, well, maybe I worded that a bit strong. Nevertheless, Cocoapods is a wonderful tool that allows you to integrate external code or your own internal projects into iOS projects. Personally, I’ve loved using Bundler in the Ruby world for years, and whenever I move to another environment, I always get a little heartbroken when I can’t find a Bundler analog.
Granted, that means that I really need to work on some personal stuff, and get out more. But nevertheless, if you’ve used Bundler, and had to develop in another platform without it, I think you know where I’m coming from.
Anyway, if you’re not using this tool yet, you need to. Really.
So what makes it so great?
I am accustomed to developing in environments that allow you to partition your codebase into separate, highly-cohesive sub-projects. If I’m developing in some other way, I start to feel dirty. And when I originally started doing iOS development, I frankly felt like a filthy, filthy man, and I did not like it. I couldn’t have separate sub-projects unless I was willing to manage my own dependency hell, which I really wasn’t. So, I tended to manually move code around from project to project or organize things in haphazardly designed Frameworks and Libraries, making things error prone and clumsy, to say the least. Then, I started using Cocoapods, and my life improved radically. I didn’t feel so dirty anymore, I was promoted, and people started to like me.
Alright, well, maybe not the last two, but I sure liked the way it let me organize my work.
How to get people to like you
Clearly, you should use Cocoapods. So how do you do this? Well, first, install Ruby. Any version in the 2.0 line should be fine. Macs come with a compliant Ruby today, so you may not even need to do this.
Then, you install Cocoapods:
(~) $ sudo gem install cocoapod
This will install the needed command line magic. Now that you have Cocoapods installed, let’s use it. First, create an XCode project (we’ll call ours Test. Original, I know). In that directory, next to the XCode project file (Test.xcodeproj), create a Podfile:
platform :ios, '9.0' use_frameworks! target 'Test' do pod 'Hue', git: 'https://github.com/hyperoslo/Hue' pod 'Imaginary', git: 'https://github.com/hyperoslo/Imaginary' pod 'Cache', git: 'https://github.com/hyperoslo/Cache' pod 'Spots', git: 'https://github.com/hyperoslo/Spots' pod 'Tailor', git: 'https://github.com/zenangst/Tailor' pod 'Fakery' end
This Podfile is tweaked just a little from one written by Zenangst, in his Hue project (another very cool iOS project; check it out). Anyway, once you have the project and podfile, run cocoapods:
(~/cocoapods) $ pod install
You should see a bunch of messages scroll across your terminal, then this, assuming your project was named Test (as it is in this example):
[!] Please close any current Xcode sessions and use `Test.xcworkspace` for this project from now on.
Cocoapods was nice enough to generate this workspace for you. Now that’s polite.
Opening the workspace, you’ll initially see two top-level items: Pods and Test. Take a look around - you can see that the original Test project has a few changes, and the Pods you outlined in the Podfile have been downloaded and are ready to build. Give it a spin, run your test project in the simulator; you’ll see that all your dependencies are compiling and are available for use.
That’s it - pretty easy to use, and great for organizing your work and dependencies, as well as other published work. For more information, check the project out at https://cocoapods.org/.
Opinions expressed by DZone contributors are their own.