Crea un Cocoapods de tu librería

Lo sentimos, este post sólo está disponible en inglés.

CocoaPods is a Dependency Manager tool written in Ruby for iOS and Mac projects. For the people who already know about RubyGems, this library is a port of RubyGems to Cocoa. Is very useful to prevent doing mechanichal work, like adding libraries to our code manually.

Installation

Cocoapods is a gem so you will need RubyGems. Once you have rubygems installed on your disk you will only need to run this command:

$ gem install cocoapods

The Podfile syntax

In every project you want Cocoapods to be your dependency manager you should create a Podfile file. The content is simple:

  1. First you configure the platform and minimum version of the SDK that it uses:

     platform :ios, 5.0
    
  2. You write a line for every dependency you have in your project.

     pod 'AFNetworking'
    
  3. If you prefer you can use a specific version of the library:

     pod 'AFNetworking', '1.2.2'
    

The result is a file like this:

    platform :ios, 5.0

    pod 'AFNetworking'
    pod 'SVProgressHUD'
    pod 'SVPullToRefresh'

Once you have your podfile you only have to run the following command:

    $ pod install

Pod install

Once this is done you start using the xcworkspace that Cocoapods creates instead of your original xcproject.

Why should I use Cocoapods?

With Cocoapods you forget about if a library is using ARC or not, how many dependencies it has and all this sort of things you may not care when doing real work. It’s all automatic. Also you will have your project more organized, having the libraries separated of your code.

Creating a Cocoapods PodSpec

So, now you know what is Cocoapods but you have your own libraries and want to create a pod for its. Let’s see how to create a podspec and updload to official repository. You can also create a podspec to use in your private repositories.

To begin check that you have installed the latest cocoapods version. To check that just type:

$ pod --version

If you have an older version (in the moment of writing this post the latest version is 0.19.1) than get the latest version using:

$ gem install cocoapods

If you want to create a auto-explained spec file you can just type:

$ pod spec create <name-of-the-pod-you-re-creating>

This will make a podspec file with a lot of comments to let you know what is necessary to create a pod. Here will talk about the mandatory fields and some optional fields:

Project name

The library name. This is autocompleted with pod spec create but you can change it you want.

s.name         =  <name-of-your-library>

Version

Cocoapods recommends using Semantic versioning for the pod specs. This version will match with the tag you have on your git repository. Cocoapods will match it automatically.

s.version      = "0.0.1"

Summary

A short description of your project

s.summary      = "A short description of your library"

Home page

Some people will use Github pages to document their libraries. Here you can specify this URL:

s.homepage     = "http://EXAMPLE/yourpod"

License type

Here you can specify the license type. CocoaPods detects automatically the license file if it is named. If you don’t know what license you should choose, you can read this complete article about Licenses.

s.license      = 'MIT (example)'    

Authors

The author is set automatically if you use git and you have set in the config. I’ve used the command and I get this:

s.author       = { "David Cortés" => "my-email@lafosca.cat" }

Source

You also need to set the git source of your project, and the tag of the current version:

 s.source       = { :git => "http://EXAMPLE/newPod.git", :tag => "0.0.1" }

Frameworks

Your library may have dependencies on other libraries. You have to write it also in the spec:

s.frameworks = 'SomeFramework', 'AnotherFramework'

Uses arc

By default your library is set to use Automatic Reference Counting. If you still use manual reference counting you should change this parameter to false:

s.requires_arc = true

These are some of the fields you can set in a Spec file but there some others. In the template file you will find it. What I did when I closed my first Pod was look in other libraries spec how they did. For example, a good example is AFNetworking spec.

Validating the spec

Once you have created the spec file you shold validate is working correctly. They have created a command for this:

$ pod spec lint MyProject.podspec

If everything is setted correctly you should see a message like that:

-> MyProject (1.2.2)

Analyzed 1 podspec.

MyProject.podspec passed validation.

Send to Cocoapods repository

We are about to finish with our first Cocoapod Library. Once we have validate it the last step is sent it to the main repository of Cocoapods. To do this you have to do a fork of Specs repository in your github account and do a Pull request of the branch you have added your podspec. Take care on follow the conventions. Every spec should be first in a folder with the pod name, and then in a folder with the pod version:

NameOfYourPod
|-> 0.0.1
|-> NameOfYourPod.podspec

And that’s all!

Hugs from lafosca

No Comments

Post a Comment