Private pod Support using CocoaPods in iOS
You might have read our recent blog on SDUI framework that introduced the concept of UI presentation made easier by eliminating the need of frequent updates for an application by the end user. It is built and deployed with just “one code base logic” and is applicable for each of the UI releases, while eliminating the need of frequent updates for the Apps.
The framework can be of use in other iOS projects; hence it requires lot of effort in manually installing and building it. Therefore to overcome this challenge the team explored various options and found a more centralized solution ‘CocoaPods’ a dependency manager tool that provides a standard format for managing the external libraries. It has over 27 thousand libraries and is used in over 1.6 million apps and can help scale any project elegantly. It focuses on source-based distribution of third party code and automatic integration into Xcode projects.
The dependencies for projects are specified in a single text file called a Podfile. CocoaPods will resolve the dependencies between libraries fetch the resulting source code then link it together in the XCode workspace to build your project.
Follow the below steps to add private pod in your iOS project:
By default CocoaPods uses public specification repository (https://github.com/CocoaPods/Specs).If you want to make your pod private then you have to create a private repository with the same structure and upload your pod specification file to private repository.
1. CocoaPods is built with Ruby and it will be installable with default Ruby available on Mac OS. Using the default Ruby install will require you to use “sudo” when installing gems.
2. Follow the below command to install Cocopods in your system:
[php]$ sudo gem install cocoapods[/php]
Create and Use CocoaPods in your iOS project
- Create a pod specification file (.podspec file)
- Specification file contains detailed information about the repository. When you execute pod install or pod update command, CocoaPods will look into specification file and clone the repository.
Follow the below steps:
1. Create specification for your pod using the following command
[php]$pod spec create <your_pod_name>[/php]
Example : pod spec create AppUtils
2. Edit the AppUtils.podspec
- Name – name of pod
- Version – current version for specification. your repo must contain a tag for version number. i.e. 1.0,1.2 etc.
- Summary – short summary of project
- Description – details description of project
- Homepage – homepage of project
- License – license for project i.e – MIT,BSD
- Author – author of repo
- Platform – OS and version used in repo. i.e. iOS 9.0
- Source – source url of repo
- source_files – files to be included in pod.
- exclude_files – files to be not included in pod.
- Dependency – any third party dependency for pod project
3. Now verify pod spec file by using the following command
[php]$ pod spec lint AppUtils.podspec[/php]
- Solve any error or warning that occur while validating .podspec file
4. Now upload specification file to specification repository.
CocoaPod is using a public specification repository for public pod. If you want to create a private pod than you have to create your own private specification repo that will store .podspec file.
Each version has its own specification file. Specification repository can have more than one specification.
5. Specifying private pod in Podfile.
If you are using private pod than you have to tell CocoaPod, where the specification repository is located by specifying source at top of the Podfile
[php]Source “<path of your private specification git repo> ”
pod ‘AppUtils’, ‘~> 1.0′[/php]
If you don’t want to create a private specification repository then create a same specification structure in your project and specify the repository URL as source in Podfile.
CocoaPods is widely used for third party dependency integration. Below are few benefits of using CocoaPod over manually managing Dependencies in your project.
- Managing dependencies in your code is simplified by downloading all of them when you run pod install/update command.
- Manually added dependency may not be easy for another coder to locate it. Pods ensure that one can easily go through the pod scheme or a Podfile to understand all the dependencies used in the project.
- Task of replacing a library with a new version is simplified with CocoaPods automatically by using only one command instead of manually deleting old files and adding new ones,
Private pods make managing your project much simpler. It saves lot of effort and time when dealing with dependencies in your project. Also we can use this pod privately for internal access.