向 Xcode 项目添加目标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7409127/
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
Adding a target to an Xcode project
提问by Paul Morris
I have an iPhone application which is currently in the app store. I want to make what is effectively exactly the same application, but which is free of charge and features ads.
我有一个 iPhone 应用程序,目前在应用程序商店中。我想制作实际上完全相同的应用程序,但它是免费的并且具有广告功能。
My plan was to effectively just copy the project, create a new one and add ads into the code and release as separate app. However, someone mentioned that the best solution would be to add an additional target to the existing application, providing two binaries at run time.
我的计划是有效地复制项目,创建一个新项目并将广告添加到代码中并作为单独的应用程序发布。然而,有人提到最好的解决方案是向现有应用程序添加一个额外的目标,在运行时提供两个二进制文件。
Seems like a good solution to me, however I am a little confused as to how I would go about altering the code to have ads built into the new target, and leaving the original untouched?
对我来说似乎是一个很好的解决方案,但是我有点困惑如何更改代码以将广告内置到新目标中,而保持原始目标不变?
采纳答案by Daniel G. Wilson
I followed thistutorial which, although old, was basically the same for xcode 4. I duplicated the target and p-list, making sure I was able to run it with changes and not affect the full version target.
我遵循了这个教程,虽然它很旧,但对于 xcode 4 来说基本上是一样的。我复制了目标和 p-list,确保我能够在更改后运行它而不影响完整版本的目标。
I then duplicated the .xib files that would be different. If you look under the project settings, somewhere you can find a list which allows you to choose which resources are included. Include the lite version's xibs in the the lite version, and the full version's in the full respectively. Then you will be able to edit each without affecting the other.
然后我复制了不同的 .xib 文件。如果您查看项目设置,您可以在某处找到一个列表,该列表允许您选择包含哪些资源。在精简版中包含精简版的xib,在完整版中分别包含精简版的xib。然后您将能够编辑每个而不影响另一个。
The icons and images can be changed in the same way. Simply create a lite version icon set or other set of images and include the lite icons in the lite target's resource settings instead of the full version's images.
图标和图像可以以相同的方式更改。只需创建一个 lite 版本图标集或其他图像集,并在 lite 目标的资源设置中包含 lite 图标,而不是完整版本的图像。
Also, you'll want to create some preprocessor macros. In the build tab look for them, and crete a macro called LITE_VERSION (or whatever you want, it doesn't really matter) for everypreprocessing option - debug, distribution and release.
此外,您还需要创建一些预处理器宏。在构建选项卡中查找它们,并为每个预处理选项 - 调试、分发和发布创建一个名为 LITE_VERSION(或任何你想要的,并不重要)的宏。
That allows you to add differing code in the same .h and .m files. Simply use
这允许您在相同的 .h 和 .m 文件中添加不同的代码。只需使用
#ifdef LITE_VERSION
// Lite version only code here
#endif
to separate the two. You can also use #ifndef LITE_VERSION
to add code only to the full version.
将两者分开。您还可以使用#ifndef LITE_VERSION
仅向完整版本添加代码。
That's it! After all of the above steps, you should be able to edit the lite version's .xib files, put code into the lite or full version only, and have separate images and icons for each.
就是这样!完成上述所有步骤后,您应该能够编辑 lite 版本的 .xib 文件,仅将代码放入 lite 或完整版本中,并且每个都有单独的图像和图标。
回答by rordulu
XenElement's answer is correct. But you should see the best practice to do it. You should have an identifier class to check for targets. If you use macros everywhere in the code, it won't seem good for you and other developers as well. In this little blog postyou can see how to create that kind of an identifier class and learn some best practices about targets in xcode.
XenElement 的回答是正确的。但是您应该看到执行此操作的最佳实践。您应该有一个标识符类来检查目标。如果您在代码中的任何地方都使用宏,那么对您和其他开发人员来说似乎也不好。在这篇小博文中,您可以了解如何创建这种标识符类,并了解有关 xcode 中目标的一些最佳实践。
回答by Thomás Calmon
I followed this Just2us: How to create multiple targets for Xcode iPhone Projectstutorial.
我遵循了这个Just2us:如何为 Xcode iPhone 项目教程创建多个目标。
I decided change step-3 with Stu's clue, setting FULL_VERSION explicitly in the paid version.
我决定根据 Stu 的提示更改步骤 3,在付费版本中明确设置 FULL_VERSION。
"To address the concern of not having accidentally LITE_VERSION defined as a macro preprocessor (thus releasing a full version accidentally), I put this little snippet of code in a header file (it just needs to be somewhere in the code base , just make sure that is common to all configurations)":
“为了解决不小心将 LITE_VERSION 定义为宏预处理器(从而意外发布完整版本)的问题,我将这个小代码片段放在头文件中(它只需要位于代码库中的某个位置,只需确保这对所有配置都是通用的)”:
#ifndef LITE_VERSION
#ifndef FULL_VERSION
#error You probably forgot to specify if this is the Lite or Full version of the app
#endif
#endif
PS: I wrote the code above right after the #import in AppDelegate.
PS:我在 AppDelegate 中的#import 之后写了上面的代码。