Profile picture

Muhammad Zeeshan

Yeah! it's me ;)

Writing custom script for Cocoapods



Cocoapods is a popular dependency manager among iOS developers. iOS developers often use Cocoapods for integrating third party libraries. It provides different hooks to perform certain custom actions. For example, post install hook will be invoked every time dependencies are resolved. Cocoapods is built with Ruby which is compatible with the preinstalled version of Ruby in Mac. In this article we are going to explore how can we write a custom script for automating certain actions. As Cocoapods is built with Ruby, we can use the Ruby language syntax and features to write custom scripts.

Real scenario:

Let say, you are using a newer version of Xcode i.e. 12 or later and you have some old dependencies to resolve and the author of the library did not update the minimum deployment target in the podspec file then you may experience the following error after running pod install. If you are developing an app for react native then most likely you will experience this issue.


The iOS Simulator deployment target ‘IPHONEOS_DEPLOYMENT_TARGET’ is set to 8.0, but the range of supported deployment target versions is 9.0 to 14.4.99. (in target ‘CocoaLibEvent’ from project ‘Pods’)


One way around is to go to each target's settings and update its deployment target manually. This way will become cumbersome if you have a large number of dependencies. Moreover, this way will become painful if you run pod install more often.


image in an article

Custom Script In Action:

A better way is to use post install hook for Cocoapods to update the target’s minimum deployment target. Whenever you run the pod install Cocoapod’s post installation hook will be invoked after resolving dependencies and here it provides the opportunity to perform custom actions using a custom script. The syntax for post installation hook is:


post_install do |installer|
	#custom script for execution


You need to put the above block inside the target's block.


Then we need to iterate all dependencies which can be done as follows:


installer.pods_project.targets.each do |target|
	#block for iterating each target


Next, we need to read the configuration settings for each target, inside the target iterator block above.


target.build_configurations.each do |config|
	#here configuration settings of the target


Next, we will read the deployment target and save it in a variable to use in the next step.


deployment_target = config.build_settings['IPHONEOS_DEPLOYMENT_TARGET']


The deployment target is a string so need to convert it into an integer for comparision. So in the next step, we will convert string into an array of characters. Before moving forward, we will add a condition for safety to make sure that the converted array has atleast 1 element to prevent the script from crashing. Then we will convert the first element into integer and compare in order to make sure that it should be greater than 9.


target_components = deployment_target.split

if target_components.length > 0
    target_initial = target_components[0].to_i
    if target_initial < 9
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = "9.0"


If we consolidate all the above steps, the script will look like this:


Inside the pod file, the script should look like the following screenshot:


image in an article


Now run the pod install in the terminal to see it in action 😎🙂


Want to get in touch? Feel free to contact me at

Muhammad Zeeshan

Copyright © 2021