如何在Podfile脚本中获取当前Xcode版本以适配不同环境?
适配不同Xcode版本的Podfile条件脚本实现
实现思路
通过命令行获取当前Xcode版本号,添加条件判断逻辑,仅在Xcode 15.0及以上版本(对应Sonoma 14.0系统适配需求)时,才注入所需的预编译宏配置,兼容Xcode 14.3的开发环境。
完整Podfile脚本示例
# 获取当前Xcode版本并转换为可比较的数值 def xcode_version version_string = `xcodebuild -version | grep "Xcode" | awk '{print $2}'` version_string.strip.to_f end post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| # 仅在Xcode 15.0及以上版本添加预编译宏 if xcode_version >= 15.0 config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', '_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION'] end end end end
代码说明
xcode_version方法:通过xcodebuild -version命令提取Xcode版本号,转换为浮点数后方便版本比较。- 条件判断:确保只有使用Xcode 15.0+的环境才会执行预编译宏的添加操作,Xcode 14.3环境不受影响,避免不必要的配置冲突。
内容的提问来源于stack exchange,提问作者Sal Jeong
相关产品推荐
相关产品推荐

