CocoaPods Swift tutorial

October 18, 2015

CocoaPods Swift tutorial

CocoaPods has became very popular among cocoa developers, due to its simplicity and tremendous help. With CocoaPods and Swift you can be very productive in your day-to-day coding life. By simply checking the name of the desired repository in CocoaPods library and including it into your Podfile is enough to start using that library. Simply as that.

Setup

First of all, you will have to install CocoaPods. It is quite simple. All you have to do is to run this command in your Terminal app, with administrator privileges:

$ sudo gem install cocoapods # Before OS X El Capitan
$ sudo gem install -n /usr/local/bin cocoapods # OS X El Capitan

Podfile

Now you have CocoaPods installed and you are almost ready to use it. Next step is to navigate to your project folder and create there file named Podfile without any extension, simply Podfile. Or you could simply write this in the Terminal app, which would do the same, while being in your project's folder:

$ pod init

In case you have created it with terminal command, delete the boilerplate code to have an empty file.
Once you have it, you will need to provide some basic info about which dependencies you want to install as well as which platform you are using. Sample Podfile would look like this:

platform :ios, '8.0'

pod 'ARSPopover', '~> 2.0'
pod 'ARSSlideTransition', '~> 1.0'

First line platform :ios, '8.0' specifies which platform your project is going to be ran on, as well as it's version. Next you have pod 'ARSPopover', '~> 2.0' line, which basically specifies the name of the pod you want to use, which most of the time is identical to the framework name, and it's upmost version number. '~> 2.0' means, that you will be pulling updates of all the versions of this framework up to version 3.0.
In case you want to download all new versions of the framework, you have to specify only name of he framework, like this:

pod 'ARSPopover'

Configuring project

After your Podilfe is up and ready, you need to run this command in Terminal app, while being inside your project's folder:

$ pod install

This will take a while, depending on you network connection and dependencies sizes. In case nothing happens or you have an error - double check that you are in the project's folder.

After this command, CocoaPods will create new file, named .xcworkspace, which will be configured to run using CocoaPods dependencies, where everything will be configured for you. From now on this should be your one and only file you to develop your app. Just forget about the regular blue .xcodeproj file from now on.

Using libraries in code

Once you have done all of the above steps and opened your new .xcworkspace file, simply navigate to you Swift file, where you want to use your new libraries, and write this line:

import ARSPopover

Aaaaand this is it, you are done!

Updating

Once in a while you will want to see if you have any updates to your dependencies. To check if there are any and to install them right away - use this command in Terminal app, while being in the project's folder:

$ pod install

Comments

comments powered by Disqus