4

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
Atticus
  • 1,365
  • 1
  • 12
  • 15
  • Hello mate, did you manage to do it? – thibaut noah Jul 06 '17 at 12:47
  • @thibautnoah I wasn't able to figure it out :(. I ended up removing the swift flag based logic and just used environment variables that I passed to the pod. – Atticus Jul 07 '17 at 19:40
  • 1
    I managed to do it, there you go mate https://stackoverflow.com/questions/44949852/access-parent-project-other-swift-flags-from-pod/44950831#44950831 – thibaut noah Jul 09 '17 at 20:05

1 Answers1

2

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
Ruzard
  • 1,167
  • 3
  • 16
  • 33