使用 CocoaPods 时如何向 Xcode 添加自定义项目配置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30102976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:07:04  来源:igfitidea点击:

How to add custom project configurations to Xcode when using CocoaPods?

iosobjective-cxcodecocoapods

提问by radex

I have an iOS/OS X Xcode projects, where I'm using CocoaPods, and I can't seem to figure out how to add my own project configurations (In addition to Debug and Release) without completely blowing up the build.

我有一个 iOS/OS X Xcode 项目,我在其中使用 CocoaPods,我似乎无法弄清楚如何在不完全破坏构建的情况下添加我自己的项目配置(除了调试和发布)。

In the project, I have a number of targets, for apps on both platforms and its app extensions. The Xcode workspace of course also has the Pods project.

在这个项目中,我有许多目标,用于两个平台上的应用程序及其应用程序扩展。Xcode 工作区当然也有 Pods 项目。

Because the project builds targets for iOS and Mac, I use CocoaPods "targets" to group their pods together. My Podfile looks something like this:

因为该项目为 iOS 和 Mac 构建目标,所以我使用 CocoaPods “目标”将它们的 pod 组合在一起。我的 Podfile 看起来像这样:

source 'https://github.com/CocoaPods/Specs.git'

target :iOS do
  platform :ios, '7.1'
  link_with 'iOS', 'NozbeToday', 'NozbeShare', 'NozbeWatch'

  # pods...
end

target :Mac do
  platform :osx, '10.9'
  link_with 'Mac'

  # pods...
end

Now here's where I have a problem. So far I've had only the default "Debug" and "Release" configurations in my project. I wanted to change them and add some new ones for different provisioning profile/bundle ID combination.

现在我遇到了问题。到目前为止,我的项目中只有默认的“调试”和“发布”配置。我想更改它们并为不同的配置文件/捆绑 ID 组合添加一些新的。

… and I'm stuck. I can't figure out how to do this.

......我被卡住了。我无法弄清楚如何做到这一点。

First sign of the problem was a warning pod installspewed out for every target/configuration combination:

问题的第一个迹象是pod install针对每个目标/配置组合发出的警告:

[!] CocoaPods did not set the base configuration of your project because your project already has a custom config set. In order for CocoaPods integration to work at all, please either set the base configurations of the target NozbeTodayto Pods/Target Support Files/Pods-iOS/Pods-iOS.dev debug.xcconfigor include the Pods/Target Support Files/Pods-iOS/Pods-iOS.dev debug.xcconfigin your build configuration.

[!] CocoaPods 没有设置你的项目的基本配置,因为你的项目已经有一个自定义的配置集。为了让 CocoaPods 集成工作,请将目标的基本配置设置NozbeTodayPods/Target Support Files/Pods-iOS/Pods-iOS.dev debug.xcconfig或包含 Pods/Target Support Files/Pods-iOS/Pods-iOS.dev debug.xcconfig在您的构建配置中。

I couldn't figure out what it means and how to fix this. Either way, the project wouldn't build — in the best case scenario I would get a linker error saying that it can't find Pods-something.a

我无法弄清楚这意味着什么以及如何解决这个问题。无论哪种方式,该项目都不会构建——在最好的情况下,我会收到一个链接器错误,说它找不到Pods-something.a……

采纳答案by radex

OK, so half-way through writing this question I figured it out myself (yay rubber ducking). Here's the solution for next generations:

好的,在写这个问题的一半时,我自己想通了(是的,橡皮鸭)。这是下一代的解决方案:

Essentially, you have to add an explicit dependency on the Pods target to your application scheme.

本质上,您必须向应用程序方案添加对 Pods 目标的显式依赖。

The way you do it, is: edit your application scheme, go to the Build section, and add the Pods target aboveyour application target. You should see something like this:

您这样做的方式是:编辑您的应用程序方案,转到构建部分,然后在您的应用程序目标上方添加 Pods目标。您应该会看到如下内容:

enter image description here

在此处输入图片说明

And it will just work.

它会起作用。



As for the CocoaPods warnings on pod install, you need to use the xcconfigfiles generated by CP for each of your Xcode configurations. The way you do it is:

至于 CocoaPods 上的警告pod install,您需要xcconfig为每个 Xcode 配置使用CP 生成的文件。你这样做的方式是:

  • find the .xcconfig files in Pods/Target Support Files
  • drag and drop them to "Pods" group in your Xcode project (add reference only. Don't copy to target or add to the build)
  • Click on the project in project navigator, and select the project itself (not one of targets). Go to Info, and under Configurations set the right .xcconfigs to each configuration and target in the "Based on Configuration file" column.
  • 找到 .xcconfig 文件 Pods/Target Support Files
  • 将它们拖放到 Xcode 项目中的“Pods”组(仅添加引用。不要复制到目标或添加到构建中)
  • 单击项目导航器中的项目,然后选择项目本身(不是目标之一)。转到“信息”,在“配置”下为“基于配置文件”列中的每个配置和目标设置正确的 .xcconfigs。

You'll also need something like this in your Podfile to let CocoaPods know which of your Xcode configurations are "debug" (unoptimized), which are "release":

您还需要在 Podfile 中添加类似内容,让 CocoaPods 知道您的 Xcode 配置中哪些是“调试”(未优化),哪些是“发布”:

project '1Nozbe', {
  'iOS 1 Dev Debug' => :debug,
  'iOS 2 Dev AdHoc' => :release,
  'iOS 3 Release Debug' => :debug,
  'iOS 4 Release AdHoc' => :release,
  'iOS 5 Release AppStore' => :release,
}


Not exactly related to CocoaPods, but if you happen to have some other (sub-project) dependencies other than CP, you'll also need to do two things:

与 CocoaPods 并不完全相关,但如果您碰巧有除 CP 之外的其他一些(子项目)依赖项,您还需要做两件事:

  • add explicit dependencies on the sub-project target (like on the screenshot above)
  • rename/add configurations in your sub-project so that they are the same as your main project. (Otherwise, Xcode just doesn't know which configuration to use with your sub-project)
  • 在子项目目标上添加显式依赖项(如上面的屏幕截图所示)
  • 在您的子项目中重命名/添加配置,以便它们与您的主项目相同。(否则,Xcode 就是不知道您的子项目要使用哪种配置)

回答by leavez

Add this line in your config file, just like import a header file:

在配置文件中添加这一行,就像导入头文件一样:

#include "Pods/Target Support Files/Pods/Pods.debug.xcconfig"

NOTE: it's #include, not #import

注意:是#include,不是#import

回答by rgkobashi

Another solution without the need to add explict dependencies to any schema nor draging and dropping stuff:

无需向任何架构添加显式依赖项或拖放内容的另一种解决方案:

How to add custom project configurations to Xcode when using CocoaPods

使用 CocoaPods 时如何向 Xcode 添加自定义项目配置

Usually when you add custom configurations to your xcode project (other than Debug and Release) the thing you shoulddo is run pod install. This will fix/remake the changes that cocoapods usually do.

通常,当您将自定义配置添加到 xcode 项目(调试和发布除外)时,您应该做的是运行pod install. 这将修复/重新制作 cocoapods 通常所做的更改。

How to get ride of CocoaPods did not set the base configuration of your project because...warning

如何乘坐CocoaPods did not set the base configuration of your project because...警告

On the Configurationssettings under Infotab, on project level, you need to set the base configuration to use (generated when running pod install)

在选项卡Configurations下的设置上Info,在项目级别,您需要设置要使用的基本配置(运行 pod install 时生成)

And dont forget to tell cocoapods what config it should use, in other words map your condfigurations with pods configurations, otherwise your compile time might dramatically increase

并且不要忘记告诉 cocoapods 它应该使用什么配置,换句话说,将您的配置映射到 pods 配置,否则您的编译时间可能会急剧增加