picture of Muhammad Zeeshan

Muhammad Zeeshan

Swift Enthusiast

Home
Blog
Tools
Github
Medium
Stack
Contact Me

Blog

Mon Dec 15 2025 ‒ 5 mins read

‒

iOS

Writing custom script for Cocoapods

MZ

Muhammad Zeeshan

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 . 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 more often.

cocoapods manually update deployment target steps

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:

code

post_install do |installer|
#custom script for execution
end

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:

code

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

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

code

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

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

code

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.

code

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"
end
end

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

code

installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
#1
deployment_target = config.build_settings['IPHONEOS_DEPLOYMENT_TARGET']
#2
target_components = deployment_target.split
#3
if target_components.length > 0
#4
target_initial = target_components[0].to_i
#5
if target_initial < 9
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = "9.0"
end
end
end
end

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

cocoapods writing custom script finalized version

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

More To Read

GMSGeocoder cancel ongoing request workaround

GMSGeocoder cancel ongoing request workaround

The process of converting latitude and longitude into human-readable addresses is called Reverse GeoCoding. If you are using GMSGeocoder for reverse geocoding then you might find yourself lost in case you need to cancel the ongoing request.

iOS