Blog

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

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 executionend
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 targetend
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 targetend
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.splitif target_components.length > 0target_initial = target_components[0].to_iif target_initial < 9config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = "9.0"endend
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|#1deployment_target = config.build_settings['IPHONEOS_DEPLOYMENT_TARGET']#2target_components = deployment_target.split#3if target_components.length > 0#4target_initial = target_components[0].to_i#5if target_initial < 9config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = "9.0"endendendend
Inside the pod file, the script should look like the following screenshot:

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