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

Cocoapods post_install, how to add target membership in Pods project

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

我认为可以在 Podfile post_install 阶段做这样的事情,但我无法弄清楚 (A) 找到 Nami.xcframework 引用我需要添加到目标,然后 (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 文件脚本,但 none 似乎完全符合我的要求。

我设法找到了我需要的完整脚本,下面的 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