Noun, plural CocoaPods
- A leathery-skinned fruit that houses a collection of cocoa beans; the origin of chocolate.
- The dependency manager for iOS projects that has thousands of libraries to help scale your projects elegantly.
Although we’re going to elaborate with the technical, iOS-related definition, many developers would argue in many ways that the latter definition is just as sweet as the former. If you’re a chocolate aficionado like myself I’m sure you’d jump at the opportunity to disagree, but hopefully this article will help even out the playing field a bit. Let’s get started.
First off, what are CocoaPods? Why should you use them? If chosen to, how do I start a projects by using CocoaPods? All perfectly valid questions.
What are CocoaPods? As stated in the definition above, CocoaPods is a tool that is used to help a developer manage their iOS project dependencies while using various external libraries in their project. While stretching to make a relation to developers that may be coming from other languages, frameworks, and environments, here are a few analogies that fall in the same class. CocoaPods is to iOS as..
NPM is to Node
Homebrew is to OSX
Gemfile is to a Ruby on Rails project.
I think you get the picture. If not, look into the three links above along with a google search on software package dependency management.
Why should you use CocoaPods? In short, using a software solution such as NPM, Homebrew, Gemfiles, or CocoaPods allow developers to have a secure place to manage their dependencies (a fancy word for libraries of software and those library’s versions.) This means that future libraries can be added/removed/updated without changing the source files in the project directly.
How do I set up an iOS project using CocoaPods?
- Make a new iOS project in Xcode.
2. Close that project before any changes are made.
3. Navigate to your project’s root folder.
4. Make a Podfile, and save it in that directory.
Input Command: $ touch Podfile
5. Include the libraries and library versions you plan to use in the project.
I am using the AFNetworking library here as an example and for use in future tutorials.
6. Install the “cocoapods” gem.
Input Command: $ gem install cocoapods
- On a side note, if you don’t have RVM installed to manage system Ruby versions and Gems, take a look at this mac installation tutorial.
7. Run “pod install” in the project root directory where the Podfile was saved.
8. After executing Step 7 you will now have the new project.xcworkspace project file that was generated by CocoaPods, as well as the original project.xcodeproj file. The project.xcworkspace file contains all of the specified dependencies from the Podfile. Continue to use this .xcworkspace file in the future when opening the project to ensure the defined dependencies are used.
That’s it! The required pods are included in the project navigator on the left hand side of the screen under the “Pods” folder. While using Xcode 5 and developing for iOS 7 applications, all that is left is to require the library’s header file in the desired project file. For example, AFNetworking would look like this above the start of the @implementation block:
Keep in mind if you are using Xcode 6, you will need to bridge the Objective-C library over into your swift project. This next step will be covered in my next post. Links coming soon!
Go forth and code!