Proper way of editing a CocoaPod Library

Mihir Mehta
7 min readFeb 1, 2019

--

This tutorial assumes that reader has basic familiarity with iOS development, Cocoapods and git.

Using Cocoapods for third-party libraries is the De facto standard in iOS development. Most of the developers who use third-party library in iOS use cocoapods. (Swift package manager does not support iOS as of now)

Cocoapods works just fine as long as your requirement is aligned with the library, but as soon as your requirement differs,you need to decide an option on how to modify the code of library and how to maintain the code afterwards

There are mainly 2 approaches that developer takes

  1. Edit the source code within pods.

The main disadvantage of this approach is if anyone runs pod update command from your team all your changes are overwritten

2. Remove library from pod and put it directly in the source code.

This overcomes the problem with first approach as the changes remain with the code as library is disconnected from pods but it has its own disadvantages.

Any bug-fixes or improvement that would be made in future in this library it will not reach to your code as its disconnected from the library.

This article will focus on the third approach which is less popular and slightly complicated but overcomes both problem. Your code will still manage by Cocoapods dependency manager and still be updated to get the bug fixes as well as it will have your custom changes which will make library work as per your requirements.

We’ll go through a live demo in which will 1. Integrate pod 2. Customise it 3. Update the library to get the bug fixes + features. All while still managing it through cocoa pods.

Create a Xcode Project Single View App named “ActivityIndicator” from XCode.

Create Project named “ActivityIndicator”

Next step is to open the terminal and go to folder where the project is created. Fire the command ‘pod init’ it will create Podfile with template. open Podfile in XCode or Sublime Text edit following

1. platform :ios, ‘12.0’ (line 2)

2. pod ‘DemoActivityIndicator’, ‘~> 3.0’ (below “# Pods for ActivityIndicator”)

Go back to terminal and fire the command ‘pod install’ . It will install the DemoActivityIndicator pods which is basically a UI Activity indicator, which looks like normal activity indicator, but additionally it accomplish following things which is normally desired with UIActivityIndicatorView.

1. It will block user interaction completely including on the UINavigationBar.

2. It will allow optional message to be displayed along with Activity Indicator.

Open Main.storyboard and drag the UIButton in your ViewController screen and set its title to Click Me

Give following constraint to Click Me button

1. Align top to Safe area with 64 margin

2. Align Center X to superview

3. Change the button fonts from System Fonts to Avenir Medium 15.0 (Later…)

Run your app and confirm that the button displays properly and when you tap on Click Me button it does show the tap affect by changing the button’s opacity.

Go to your ViewController.swift file and write following code in viewDidLoad function

Run your app. It will display 2 activity indicators one by one in the center and also notice that it will not let you tap on Click Me button while activity indicator is running. cool right ?

But it has one limitation though

As your “entire” app 😉 is using Avenir fonts, the activity indicator with message uses the system fonts. Hmmmm…. the first thought that comes to your mind is directly updating label’s fonts inside library but if you do that you’re risking it to be overwritten when you or one of your colleague runs pod update. The second option is to download the source code directly from github and drag the folder directly to your project and edit the fonts but in that case you will miss all the future bug fixes and new feature that are likely to be added in the library.

We will take the third approach in which we’ll edit the library while code remain in cocoapods framework which cannot be overwritten and also we’ll get all the future bug fixes + new features.

visit DemoActivityIndicator github page https://github.com/mihirpmehta/DemoActivityIndicator and fork the repository to your github page. clone it locally

ALERT: Following step is required only for this tutorial and not normally. As the current repository already has all the improvements and new features, we will move back the repository to previous version, modify the code and than merge it with latest version. open your terminal and go to the location where you have forked the DemoActivityIndicator. fire the following command on terminal

git reset --hard f84b8e2314dbe452156ed387b1ac37399d3d4319

It will move your repository back to previous version.

Now create and push a new branch for your forked repository and name in Improvement. Now open Example/DemoActivityIndicator.xcworkspace in Xcode. open UIView+Extension.swift replace

label.font = UIFont.systemFont(ofSize: 15.0)

with

label.font = UIFont(name: “Avenir-Medium”, size: 15.0)

commit and push your change to your new branch.

Now go to your github.com and open your forked repository, click on list of commits and find the latest commits in which you have updated the fonts. Copy the SHA of that commit.

Copy SHA

Now open your actual project ActivityIndicator and open the Podfile replace

with

add :git => “URL to your repo” and :commit => “Your SHA”

open terminal, navigate to your ActivityIndicator project and fire ‘pod update’ command. It will update repository which will have custom fonts as per your requirement.

Cool… 😎 You still have cocoapods for maintaining your third party library which also has custom code as per your requirement.

This is fine but we still need to overcome an issue. What if the library fixes its bugs or add new features or even migrate from Swift 3 to Swift 4, you will miss those future updates in this case. Let’s fix this as well.

Now open your terminal and update your master branch to check if the original library has some update (In fact it has because in previous step we move our forked repository to previous version… 😉)

# Fetch all the branches of that remote into remote-tracking branches,
# such as upstream/master:

git fetch upstream

# Make sure that you're on your master branch:

git checkout master

# Rewrite your master branch so that any commits of yours that
# aren't already in upstream/master are replayed on top of that
# other branch:

git rebase upstream/master

As there are lot of improvement on the latest version, your forked repository will get all the updates from the original library.

Now merge your Improvement branch to your master branch and push it on github (I am using Github Desktop for mac for merging. You can use command line as well)

Remember you need to create the branch and merge it back only for this tutorial because we are not using latest version of library otherwise these steps are not needed in real life scenario (Branching and merging).

Open your forked repository on github and copy the SHA of your last merge/commit. update your Podfile with new commit and fire ‘pod update’ on terminal

You will get library with latest update in which you can customise background color, font color and activityIndicator color with newly introduced ActivityConfiguration class.

update your ViewController.swift file with following code and run your app.

It will display activity indicator with custom colors as well as it will use Avenir fonts instead of system fonts which is default fonts of your app 😉.

As someone said a picture is worth a thousand words. Let’s see a flowchart which tries to explain the entire process in single chart

With this exercise you should be able to customise any third party library and also get future update and bug fixes from it while maintaining all through cocoapods. And you do not need to forgo either customisation or future update and bug fixes.

--

--