How to set the Legacy Swift Versions for each Pod in Podfile

Solution 1:

I found this while searching how to set the SWIFT_VERSION to 3.2 for Xcode 9.

Using the AirMapSDK which itself and multiple dependencies need 3.2 instead of 4.0 Here is an example of how to set pod specific SWIFT_VERSION for others that may come across this question.

post_install do |installer|
  installer.pods_project.targets.each do |target|
    if ['AirMapSDK', 'PhoneNumberKit', 'Lock', 'RxSwift', 'RxSwiftExt', 'RxCocoa', 'RxDataSources', 'ProtocolBuffers-Swift'].include? target.name
      target.build_configurations.each do |config|
        config.build_settings['SWIFT_VERSION'] = '3.2'
      end
    end
  end
end

You can change the if array to specify whatever pod you need to set the version too.

Solution 2:

If you need to manage multiple Swift versions for pods, you can build a mapping.

DEFAULT_SWIFT_VERSION = '4'
POD_SWIFT_VERSION_MAP = {
  'Dotzu' => '3'
}

post_install do |installer|
  installer.pods_project.targets.each do |target|
    swift_version = POD_SWIFT_VERSION_MAP[target.name] || DEFAULT_SWIFT_VERSION
    puts "Setting #{target.name} Swift version to #{swift_version}"
    target.build_configurations.each do |config|
      config.build_settings['SWIFT_VERSION'] = swift_version
    end
  end
end

Solution 3:

Details

  • CocoaPods v1.6.1
  • Xcode 10.2.1 (10E1001)

Ruby code in podfile

swift_versions_of_pods = { 'swiftScan' => '4.0', 'GRDB.swift' => '4.2' }
post_install do |installer|
  installer.pods_project.targets.each do |target|
    defined_swift_version = swift_versions_of_pods[target.name]
    next if defined_swift_version.blank?
    target.build_configurations.each do |config|
      config.build_settings['SWIFT_VERSION'] = defined_swift_version
    end
  end
end

Solution 4:

If you want to use different Swift versions, you can use this:

def pods

  pod 'PodWithSwift40'
  pod 'Pod1WithSwift42'
  pod 'Pod2WithSwift42'

end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    if ['Pod1WithSiwft42', 'Pod2WithSiwft42'].include? target.name
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '4.2'
        end
    else
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '4.0'
        end
    end
  end
end