Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use OTHER_SWIFT_FLAGS in a Pod

Has anybody tried to use build configuration flags in a Swift based Pod? I am trying to use a post_install hook to add "OTHER_SWIFT_FLAGS" to a private Swift pod that I'm working on. Basically, I want to be able to do something like #if DEV #elseif QA #else #endif to change endpoints based on the target that I'm using. The project I'm integrating the pod with also uses DEV & QA flags which work fine with corresponding DEV & QA targets.

Here is a post_install hook that I came up using help from other similar posts:

post_install do |installer|
    installer.pods_project.targets.each do |target|
        if target.name == 'Pods-Test-QA'
            puts "Found #{target.name}. Adding Swift Flag..."
            target.build_configurations.each do |config|
                puts "Configuration: #{config}"
                config.build_settings['OTHER_SWIFT_FLAGS'] ||= ['$(inherited)']
                config.build_settings['OTHER_SWIFT_FLAGS'] << '-DQA'
                puts "#{config.build_settings['OTHER_SWIFT_FLAGS'].inspect}"
            end
        end
        if target.name == 'Pods-Test-DEV'
            puts "Found #{target.name}. Adding Swift Flag..."
            target.build_configurations.each do |config|
                puts "Configuration: #{config}"
                config.build_settings['OTHER_SWIFT_FLAGS'] ||= ['$(inherited)']
                config.build_settings['OTHER_SWIFT_FLAGS'] << '-DDEV'
                puts "#{config.build_settings['OTHER_SWIFT_FLAGS'].inspect}"
            end
        end
    end
end

After running pod install the post_install successfully adds the flags as shown in the image below for Pods-Test-QA & Pods-Test-DEV.

enter image description here

However, when I run the project using QA or DEV targets and use the following flags it always hits PROD:

#if DEV
    print("DEV ENVIRONMENT")
#elseif QA
    print("QA ENVIRONMENT")
#else
    print("PROD ENVIRONMENT")
#endif

Is there something I'm missing? Also, for some reason Pods-Test-QA.debug.xcconfig & DEV don't seem to be updated with the flags I've added. Does anyone have any idea why it's not updating in the .xcconfig files?

OTHER_SWIFT_FLAGS = $(inherited) "-D" "COCOAPODS"

Any help would be appreciated. Thanks in advance.

Using:

  • cocoapods (1.0.1)
  • Xcode 7.3.1
like image 470
Atticus Avatar asked Oct 15 '25 18:10

Atticus


1 Answers

If you develop your own pod and would like to add some flags depending on the configuration you can do so by mentioning it in your podspec file.

My goal was to add 'BETA' flag to my pod if parent's project active configuration contains 'BETA' in its name without post-install hooks.

s.pod_target_xcconfig = {
    'OTHER_SWIFT_FLAGS[config=*Beta*]' => '-DBETA',
}

Podspec not only allows to do add custom flags but gives an opportunity to use wildcards.

The result: if my parent project uses 'Extra Early Beta' configuration then the following fork in pod's sources goes to 'BETA'

        #if BETA
        // Beta related code
        #else
        // Code I don't need in beta.
        #endif
like image 136
Ruzard Avatar answered Oct 17 '25 07:10

Ruzard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!