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

问题描述 投票:0回答:1

我有一个Podfile,当构建Pods.xcodProj时,最终包含了一个xcframework,这是一个Pods.xcodeproj文件的引用,我需要将其添加为一个目标引用到Pods构建的目标之一。

我认为可以在Podfile中做这样的事情。post_install 阶段,但我想不通的是 xcodeproj gem语法要求(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文件脚本的例子,但没有一个是我想做的。

Screen shot showing framework and target membership I need to adjust in Podfile

ios xcode cocoapods xcode-project xcodeproj
1个回答
0
投票

我设法找出了我需要的完整脚本,下面的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
© www.soinside.com 2019 - 2024. All rights reserved.