如何从 Xcode 中的相同共享代码创建多个应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7454946/
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
How to create multiple apps from same shared code in Xcode?
提问by Slee
I'm developing 2 different apps that share 95% of the same code and views. What is the best way to go about this using Xcode?
我正在开发 2 个不同的应用程序,它们共享 95% 的相同代码和视图。使用 Xcode 解决此问题的最佳方法是什么?
回答by AliSoftware
Use targets. That's exactly what they are for.
使用目标。这正是他们的目的。
Learn more about the concept of targets here.
Typically, the majority of projects have a single Target, which corresponds to one product/application. If you define multiple targets, you can:
通常,大多数项目都有一个 Target,它对应于一个产品/应用程序。如果定义多个目标,则可以:
- include some of your source code files (or maybe all) in both targets, some in one target and some in the other
- you can play with Build Settings to compile the two targets using different settings.
- 在两个目标中都包含一些(或全部)源代码文件,一些在一个目标中,一些在另一个目标中
- 您可以使用构建设置来使用不同的设置编译两个目标。
For example you may define Precompiler Macros for one target and other macros for the other (let's say OTHER_C_FLAGS = -DPREMIUM
in target "PremiumVersion" and OTHER_C_FLAGS = -DLITE
to define the LITE
macro in the "LiteVersion" target) and then include similar code in your source:
例如,您可以为一个目标定义预编译器宏,为另一个目标定义其他宏(假设OTHER_C_FLAGS = -DPREMIUM
在目标“PremiumVersion”中并在“LiteVersion”目标中OTHER_C_FLAGS = -DLITE
定义LITE
宏),然后在源中包含类似的代码:
-(IBAction)commonCodeToBothTargetsHere
{
...
}
-(void)doStuffOnlyAvailableForPremiumVersion
{
#if PREMIUM
// This code will only be compiled if the PREMIUM macro is defined
// namely only when you compile the "PremiumVersion" target
.... // do real stuff
#else
// This code will only be compiled if the PREMIUM macro is NOT defined
// namely when you compile the "LiteVersion" target
[[[[UIAlertView alloc] initWithTitle:@"Only for premium"
message:@"Sorry, this feature is reserved for premium users. Go buy the premium version on the AppStore!"
delegate:self
cancelButtonTitle:@"Doh!"
otherButtonTitles:@"Go buy it!",nil]
autorelease] show];
#endif
}
-(void)otherCommonCodeToBothTargetsHere
{
...
}