ios

cocoapod自定义BuildSetting设置

2018-08-23  本文已影响413人  小杰杰杰

想着消除工程里的一些警告,就研究了下cocoapod的一些功能,

我们可以使用inhibit_all_warnings!来忽略pod里引用的第三方库的警告

然后还有一些其他的警告:
最常见的警告是Validate Project Settings

image1.png
这其实是iOS Deployment Target设置在作怪
在工程内选择DTCoreText Target查看
image2.png

这里iOS Deployment Target 4.3跟我们项目最低支持8.0系统的配置不一样,
我在Podfile里已经配置了platform :ios, '8.0',是什么导致这样的情况呢?
我们去Github上看看DTCoreText/DTCoreText.podspec

image.png
第四行spec.platforms = {:ios => '4.3', :tvos => '9.0' }是只DTCoreText支持iOS4.3以上的系统,cocoapod根据这个设置iOS Deployment Target不知道是bug还是feature

点击 image1.pngUpdate to recommended settings XCode会弹出推荐配置

image.png
选择Perform Changes
xcode会把所有不匹配的iOS Deployment Target设置一遍。但是这样每次pod install 一次后得手动设置未免有些繁琐。
下面给cocoapod添加自定义配置:
post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '8.0'
    end
  end
end

也可以给cocoapod设置一些自定义的编译选项:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['STRIP_INSTALLED_PRODUCT'] = 'YES'
      config.build_settings['STRIP_STYLE'] = 'non-global'
      config.build_settings['DEPLOYMENT_POSTPROCESSING'] = 'YES'
    end
  end
end

Xcode Build Settings显示的iOS Deployment Target跟实际的key:IPHONEOS_DEPLOYMENT_TARGET不一样,获取真实key的方式的方式很简单:
image2.png 选中蓝色iOS Deployment Target 按下复制(command + c)
然后粘贴到空白处,就能看到真实的key:

//:configuration = Debug
IPHONEOS_DEPLOYMENT_TARGET = 8.0

//:configuration = Release
IPHONEOS_DEPLOYMENT_TARGET = 8.0

//:completeSettings = some
IPHONEOS_DEPLOYMENT_TARGET

如果你想只针对Release设置,那么这样:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
        if config.name == "Release"
            config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '8.0'
        end
    end
  end
end

其他例子

if config.name == "Debug" then
  //do something
elsif config.name == "Release" then
  //do something
end

最后别忘了执行pod install
enjoy!

上一篇 下一篇

猜你喜欢

热点阅读