Cocoapods post install script for updating deployment target
While developing apps with react native we often need to run pod install
to resolve dependencies. If you are using newer version of xcode i.e. 12 or latter, you may experience the following error after running pod install.
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’)
For example, react native use Flipper as dependency. When you run pod install its deployment target is reset to 8.0 which is not supported by the newer version of xcode.
To resolve this issue we have 2 choices:
Manual
We have to manually goto each target’s build setting and update it’s deployment target.
This manual method might get painful if you run pod install
more often.
Automated
We can use post install hook for cocoapods to update the target’s deployment target.
Whenever you run the pod install
cocoapod’s post installation hook will be invoked and you can conditionally set the deployment targets only if needed. The script looks like this:
Copy and paste it in your pod file under your target’s section and run pod install to see it in action. Finally your pod file might look something like this:
If you are curious to know how it works read on:
Cocoapods is using ruby script so you can use its feature here as well.
- Read and save the deployment target in a variable from the target’s build settings. The build settings is based on the predefined keys. I found the key (IPHONEOS_DEPLOYMENT_TARGET) from the error, see the first screenshot above you will get the clue.
- Deployment target is a string value, for comparison we need to convert it to a number. As we need to update the deployment target value only if it is less than 9. For this we split the string into an array.
- Next we check the string components length should be greater than 0 in order to proceed.
- Moving forward we get the first element from the array and convert it into integer.
- Finally we check if it is less than 9 then update it otherwise do nothing.
That’s it! Let me know your thoughts in the comment section below. Cheers 🙂