繁体   English   中英

Cocoapods post_install,如何在 Pods 项目中添加目标成员资格

[英]Cocoapods post_install, how to add target membership in Pods project

我有一个 Podfile,在构建 Pods.xcodeProj 时最终会包含一个包含的 xcframework,它是一个 Pods.xcodeproj 文件引用,我需要将其作为目标引用添加到其中一个 pod 构建目标。

我认为可以在 Podfile post_install阶段做这样的事情,但我无法弄清楚 (A) 找到我需要添加到目标的 Nami.xcframework 引用所需的xcodeproj gem 语法,然后 (B) 添加该文件参考所需的目标(有关我希望调整目标成员资格的框架,请参见下图,我基本上只是想自动检查该目标成员资格框)。

我对这个 Podfile 脚本的开始看起来像:

post_install do |installer|
    nami_target = installer.pods_project.targets { |f| f.name == "react-native-nami-sdk" }

    #Pseudocode begins here, this is what I cannot figure out
    nami_xcframework_fileref = ??
    nami_target.addBuildReference(nami_xcframework)
end

感谢您对此的任何帮助,我找到了许多示例 pod 文件脚本,但似乎没有一个完全符合我的要求。

显示我需要在 Podfile 中调整的框架和目标成员资格的屏幕截图

我设法找出了我需要的完整脚本,下面的 Podfile post_install 脚本正是我想要的。

请注意,关键是虽然您可以使用.name属性检查目标,但对于文件引用, .path将始终包含您可以检查的内容, .name通常为空白。 另一个关键项目是您需要将文件引用添加到目标的frameworks_build_phase方面。

最终脚本(添加到 Podfile 的末尾):

post_install do |installer|
  puts("Attempting to add Nami.xcframework reference to react-native-nami-sdk project.")
  installer.pods_project.targets.each do |target|
    if target.name  == "react-native-nami-sdk"
      puts("Found react-native-nami-sdk target.")
      all_filerefs = installer.pods_project.files
      all_filerefs.each do |fileref|
         if fileref.path.end_with? "Nami.xcframework"
          puts("Found Nami.xcframework fileref.")
          build_phase = target.frameworks_build_phase
          puts("Determining if react-native-nami-sdk build phase needs correction.")
          unless build_phase.files_references.include?(fileref)
            puts("Adding Nami.xcframework to react-native-nami-sdk target")
            build_phase.add_file_reference(fileref)
          end
         end
      end
    end
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM