ios - How can I modify OTHER_LDFLAGS via CocoaPods post-install hook? -


my project uses cocoapods , custom xcconfig files. until now, hasn't caused problems: i've had #include cocoapods-generated configuration @ end of custom configuration.

however, i've run problem need conditionally specify other_ldflags based on xcconfig, can't figure out how this.

as start, i've tried logging other_ldflags this, flags aren't logged:

post_install |installer_representation|   installer_representation.project.targets.each |target|     target.build_configurations.each |config|              name = target.name       puts "target found: #{name}"        flags = config.build_settings['other_ldflags']       puts "other_ldflags found: #{flags}"     end   end end 

the output looks this:

target found: pods-projectname-dependencyname1 other_ldflags found: # nothing here...? target found: pods-projectname-dependencyname2     other_ldflags found: # again nothing... # etc... target found: pods-projectname  # cool, main target pod other_ldflags found: # ... 

how can modify other_ldflags via cocoapods post-install hook?

i stumbled across same problem. first tried modify other_ldflags obvious:

post_install |installer|     installer.pods_project.targets.each |target|         if target.name == "pods-sometarget"             puts "updating #{target.name} other_ldflags"             target.build_configurations.each |config|                 config.build_settings['other_ldflags'] ||= ['$(inherited)']                 config.build_settings['other_ldflags'] << '-l"afnetworking"'             end         end     end end 

but didn't work. relevant xcconfig didn't change. found workaround works - first read relevant xcconfig file content in post_intall hook, modify , write back:

v1.0

post_install |installer|     installer.pods_project.targets.each |target|         if target.name == "pods-sometarget"             puts "updating #{target.name} other_ldflags"             target.build_configurations.each |config|                 xcconfig_path = config.base_configuration_reference.real_path                 xcconfig = file.read(xcconfig_path)                 new_xcconfig = xcconfig.sub('other_ldflags = $(inherited)', 'other_ldflags = $(inherited) -l"afnetworking"')                 file.open(xcconfig_path, "w") { |file| file << new_xcconfig }             end         end     end end 

edit: improvement on v1.0. instead of operating on xcconfig string content directly, read xccconfig build_configuration hash, modify hash , flush xcconfig.

v1.5

post_install |installer|     installer.pods_project.targets.each |target|         if target.name == "pods-sometarget"             puts "updating #{target.name} other_ldflags"             target.build_configurations.each |config|                 xcconfig_path = config.base_configuration_reference.real_path                  # read xcconfig build_settings dictionary                 build_settings = hash[*file.read(xcconfig_path).lines.map{|x| x.split(/\s*=\s*/, 2)}.flatten]                  # modify other_ldflags                 build_settings['other_ldflags'] << '-l"afnetworking"'                  # write build_settings dictionary xcconfig                 build_settings.each |key,value|                   file.open(xcconfig_path, "a") {|file| file.puts key = value}                 end             end         end     end end 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -