what is cocoapods and how to setup?
TRANSCRIPT
Milan Panchal iOS Developer
B L O G : h t t p : / / w w w . j e e n a l i n f o t e c h . c o m /
iOS - Cocoa Pods
1
Cocoa Pods By Milan Panchal
Agenda
What is CocoaPods?
Why use CocoaPods?
Installing CocoaPods
Using CocoaPods in a project
CocoaPods Versioning
CocoaPods and source control
What is Podfile.lock?
Other
2
Cocoa Pods By Milan Panchal
What is CocoaPods?3
CocoaPods is a dependency management tool for iOS (Objective-C/Swift) and OS X development. Eg. Maven for Java Projects.
It is built with Ruby so we can install through default Ruby available on OS X.
It has thousands of libraries and can help you scale your projects elegantly.
Cocoa Pods By Milan Panchal
Why we use CocoaPods?4
You begin working on a project and everything is going great. As the project progresses, you find it necessary to use third party libraries to help the project move along faster.
You do this by searching for appropriate libraries and copying the source code into your project.
When a library is updated, you manually update it in your project. At times, a library might break your build, forcing you to revert back to a compatible version.
Cocoa Pods By Milan Panchal
Why we use CocoaPods? (Continue.)5
You have to search for it and copy it into your project. Add more libraries and you start noticing the time drain that maintaining your project dependencies is.
You are also bound to encounter situations where a library you want to use depends on another library and you will have to get them both.
If you use multiple libraries sharing a dependency, but all specify a different version of the dependency, you’ll have to figure out what the best version of the library is that will satisfy all dependencies.
Cocoa Pods By Milan Panchal
Why we use CocoaPods? (Continue.)6
As your project scales, you’ll be spending a lot of time managing its dependencies, time that could be better used writing code.
CocoaPods make managing dependencies in your code easier. Adding and removing dependencies is just a matter of defining them in a file and running a command to install them.
CocoaPods reads the file, determines the dependencies that the listed libraries have and if there are any shared dependencies, it tries to determine the version that will satisfy all of them. It uses Semantic Versioning to try to resolve dependency versions.
Cocoa Pods By Milan Panchal
Installing CocoaPods7
CocoaPods is distributed as a Ruby gem and so will require Ruby and RubyGems (the Ruby package manager) to be installed on your system.
To check whether Ruby is installed, run the ruby -vcommand in the Terminal.
Before installing CocoaPods, first update your RubyGems.
$ [sudo] gem update --system
To install CocoaPods, run the following command.$ [sudo] gem install cocoapods
Cocoa Pods By Milan Panchal
Installing CocoaPods (Continue)8
Next run the following to setup CocoaPods on you system.
$ pod setup [--verbose]
This will create a ~/.cocoapods/ directory which will contain all the available public pod specifications cloned from the CocoaPods Master Specs Repo.
This process will likely take a few minutes as it clones the CocoaPods Master Specs repository into ~/.cocoapods/ on your computer.
Cocoa Pods By Milan Panchal
Installing CocoaPods (Continue)9
The verbose option logs progress as the process runs, allowing you to watch the process instead of seeing a seemingly “frozen” screen.
Now we are all set up and ready to use CocoaPodsin our project.
Cocoa Pods By Milan Panchal
Using CocoaPods (Continue)10
To create the Podfile, using Terminal, navigate to the root of the app you created.
$ cd /Path/to/Project/TestApp
Run the following command, which will create a Podfile at the specified file path.
$ pod init
Type this command to open the Podfile using Xcodefor editing:
$ open -a Xcode Podfile
Cocoa Pods By Milan Panchal
Using CocoaPods (Continue)11
The default Podfile looks like this:
# Uncomment this line to define a global platform for your project# platform :ios, "8.0”
target "TestApp" dopod 'iOS-Category'pod 'iOS-Category', '~> 1.0.4’
end
target "TestAppTests" do
end
Cocoa Pods By Milan Panchal
Using CocoaPods (Continue)12
The platform can be either ios or osx.
Add a CocoaPod by specifying pod '$PODNAME' on a single line
After editing the Podfile, save it and run the following command.
$ pod install
Cocoa Pods By Milan Panchal
Using CocoaPods (Continue)13
Inhibits all the warnings from the CocoaPods libraries.
Add the following to the top of Podfile to tell CocoaPodsto silence all warnings from pod dependencies:
$ inhibit_all_warnings!
If you would like to inhibit warnings per Pod you can use the following syntax:
$ pod 'iOS-Category', :inhibit_warnings => true
Cocoa Pods By Milan Panchal
Using CocoaPods (Continue)14
use_frameworks!
platform :ios, "8.0"
use_frameworks!
This tells CocoaPods that your project is targeting iOS 8.0 and will be using frameworks instead of static libraries.
Cocoa Pods By Milan Panchal
Using CocoaPods (Continue)15
generate_bridge_support!
Specifies that a BridgeSupport metadata document should be generated from the headers of all installed Pods.
set_arc_compatibility_flag!
Specifies that the -fobjc-arc flag should be added to the OTHER_LD_FLAGS.
Cocoa Pods By Milan Panchal
CocoaPods Versioning16
CocoaPods follows the Semantic Versioning policy. You can read more about major, minor and patch versions at the given link.
When the version number is not specified, the latest version of the pod will be fetched.
You can specify an exact version by including a specific version number.
For instance, pod 'iOS-Category', '1.0.4’
Cocoa Pods By Milan Panchal
CocoaPods Versioning (Continue)17
Besides no version, or a specific one, it is also possible to use operators:
• > 0.1 Any version higher than 0.1.• >= 0.1 Version 0.1 and any higher version.• < 0.1 Any version lower than 0.1.• <= 0.1 Version 0.1 and any lower version.• ~> 0.1.2 Version 0.1.2 and the versions up to 0.2, not
including 0.2. This operator works based on the last component that you specify in your version requirement. The example is equal to >= 0.1.2 combined with < 0.2.0 and will always match the latest known version matching your requirements.
Cocoa Pods By Milan Panchal
Pods and Source Control18
There are various arguments on what CocoaPodsfiles should go under version control. This is simply a matter of preference.
There is a list of pros and cons of adding the Pods folder in your .gitignore file.
You should however keep the Podfile.lock under version control. This is the file that keeps track of what version of a Pod is installed.
Cocoa Pods By Milan Panchal
Pods and Source Control (Continue)19
Benefits of ignoring the Pods directory
The source control repo will be smaller and take up less space.
As long as the sources (e.g. GitHub) for all Pods are available, CocoaPods is generally able to recreate the same installation. (Technically there is no guarantee that running pod install will fetch and recreate identical artifacts when not using a commit SHA in the Podfile. This is especially true when using zip files in the Podfile.)
There won't be any conflicts to deal with when performing source control operations, such as merging branches with different Pod versions.
Whether or not you check in the Pods directory, the Podfile and Podfile.lock should always be kept under version control.
Cocoa Pods By Milan Panchal
Pods and Source Control (Continue)20
Benefits of checking in the Pods directory
• After cloning the repo, the project can immediately build and run, even without having CocoaPods installed on the machine.
• There is no need to run pod install, and no Internet connection is necessary.
• The Pod artifacts (code/libraries) are always available, even if the source of a Pod (e.g. GitHub) were to go down.
• The Pod artifacts are guaranteed to be identical to those in the original installation after cloning the repo.x
Cocoa Pods By Milan Panchal
What is Podfile.lock?21
This file is generated after the first run of pod install, and tracks the version of each Pod that was installed.
For example, imagine the following dependency specified in the Podfile:
pod 'iOS-Category'
Running pod install will install the current version of iOS-Category, causing a Podfile.lock to be generated that indicates the exact version installed (e.g. iOS-Category 1.0.5).
Cocoa Pods By Milan Panchal
What is Podfile.lock? (Continue)22
Thanks to the Podfile.lock, running pod install on this hypothetical project at a later point in time on a different machine will still install iOS-Category 1.0.5 even if a newer version is available. CocoaPods will honour the Pod version in Podfile.lock unless the dependency is updated in the Podfile or pod update is called (which will cause a new Podfile.lock to be generated). In this way CocoaPods avoids headaches caused by unexpected changes to dependencies.
This file should always be kept under version control.
Cocoa Pods By Milan Panchal
Other23
Using the files from a local path or repo (GIT/SVN)
From Local Machine:pod 'ExamplePod', :path => '~/Documents/ExamplePod'
From a git repository:
To use the master branch of the repository:pod 'ExamplePod', :git => 'https://github.com/eguser/ExamplePod.git'
To use a different branch of the repository:pod 'ExamplePod', :git => 'https://github.com/eguser/ExamplePod.git', :branch => 'dev'
To use a tag of the repository:pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :tag => '0.7.0'
Or specify a commit:pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :commit =>
'082f8319af’
Cocoa Pods By Milan Panchal
Other (Continue)24
Trying Out Pods
CocoaPods has a feature that enables you to download and try out a library before you use it in your project. It downloads the demo project of a Pod and opens it in Xcode.
Use the pod try [LIB_NAME] command to try out a Pod.
pod try iOS-Category
Cocoa Pods By Milan Panchal
Other (Continue)25
Installing The Same Pod In Multiple Targets
The Ugly But Works Solution
# Podfileplatform :ios, ‘8.0’use_frameworks!
# My other podstarget 'MyTests' do
pod 'Quick', '0.5.0'pod 'Nimble', '2.0.0-rc.1'
end
target 'MyUITests' dopod 'Quick', '0.5.0'pod 'Nimble', '2.0.0-rc.1'
end
Cocoa Pods By Milan Panchal
Other (Continue)26
Installing The Same Pod In Multiple Targets
The Elegant Solution
platform :ios, ‘8.0’use_frameworks!
# My other podsdef testing_pods
pod 'Quick', '0.5.0’pod 'Nimble', '2.0.0-rc.1’
end
target 'MyTests' dotesting_pods
endtarget 'MyUITests' do
testing_podsend
Cocoa Pods By Milan Panchal
Other (Continue)27
Semantic Versioning:
• A normal version number MUST take the form X.Y.Z where X, Y, and Z are non-negative integers, and MUST NOT contain leading zeroes. X is the major version, Y is the minor version, and Z is the patch version. Each element MUST increase numerically. For instance: 1.9.0 -> 1.10.0 -> 1.11.0
Cocoa Pods By Milan Panchal