ios - 如何通过 CocoaPods 安装后 Hook 修改 OTHER_LDFLAGS?

标签 ios ruby cocoapods

我的项目使用 CocoaPods 和自定义 xcconfig 文件。到目前为止,这还没有造成任何问题:我只需要在自定义配置的末尾#include CocoaPods 生成的配置。

但是,我遇到了一个问题,需要根据 xcconfig 有条件地指定 OTHER_LDFLAGS,但我不知道该怎么做。

一开始,我试过像这样简单地记录 OTHER_LDFLAGS,但实际上并没有记录标志:

post_install do |installer_representation|
  installer_representation.project.targets.each do |target|
    target.build_configurations.each do |config|      

      name = target.name
      puts "Target Found: #{name}"

      flags = config.build_settings['OTHER_LDFLAGS']
      puts "OTHER_LDFLAGS Found: #{flags}"
    end
  end
end

输出看起来像这样:

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, this is the main target pod
OTHER_LDFLAGS Found: # ...

如何通过 CocoaPods 安装后 Hook 实际修改 OTHER_LDFLAGS

最佳答案

我遇到了同样的问题。首先,我尝试用显而易见的方式修改 OTHER_LDFLAGS:

post_install do |installer|
    installer.pods_project.targets.each do |target|
        if target.name == "Pods-SomeTarget"
            puts "Updating #{target.name} OTHER_LDFLAGS"
            target.build_configurations.each do |config|
                config.build_settings['OTHER_LDFLAGS'] ||= ['$(inherited)']
                config.build_settings['OTHER_LDFLAGS'] << '-l"AFNetworking"'
            end
        end
    end
end

但是没有用。相关的 xcconfig 没有得到改变。最终我找到了一个很好的解决方法——首先读取post_intall钩子(Hook)中的相关xcconfig文件内容,修改并写回:

v1.0

post_install do |installer|
    installer.pods_project.targets.each do |target|
        if target.name == "Pods-SomeTarget"
            puts "Updating #{target.name} OTHER_LDFLAGS"
            target.build_configurations.each do |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

编辑:对 v1.0 的改进。不是直接对 xcconfig String 内容进行操作,而是将 xccconfig 读入一个 build_configuration Hash,修改 hash 然后刷新到 xcconfig。

v1.5

post_install do |installer|
    installer.pods_project.targets.each do |target|
        if target.name == "Pods-SomeTarget"
            puts "Updating #{target.name} OTHER_LDFLAGS"
            target.build_configurations.each do |config|
                xcconfig_path = config.base_configuration_reference.real_path

                # read from xcconfig to 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 to xcconfig
                build_settings.each do |key,value|
                  File.open(xcconfig_path, "a") {|file| file.puts key = value}
                end
            end
        end
    end
end

关于ios - 如何通过 CocoaPods 安装后 Hook 修改 OTHER_LDFLAGS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30244675/

相关文章:

ruby-on-rails - Ruby on Rails CRUD 和用户认证授权模块

ios - 在 XCode 单元测试中测试导航

ios - 使用 asynchImageView 调用网络服务

arrays - Ruby:根据值替换数组中的元素

ruby-on-rails - 在方法参数中传递大对象是否可以?

swift - Obj-C CocoaPods + Swift 框架

ios - Xcode Playground 与 Cocoapods

ios - 如何在 iOS 中暂停/恢复/取消事件

android - OpenGL ES 2.0 : Unable to perform simple rotation

ios - 如果框架不存在,Objective-c 在运行时关闭类/代码块