如何在Xcode中从相同的共享代码创build多个应用程序?

我正在开发2个不同的应用程序,共享95%的相同的代码和意见。 什么是最好的方式去使用Xcode这个?

使用目标。 这正是他们的目的。

在这里了解更多关于目标的概念 。

通常情况下,大多数项目都有一个目标,对应于一个产品/应用程序。 如果您定义了多个目标,您可以:

  • 在两个目标中包含一些源代码文件(或者全部),一些在一个目标中,另一些在另一个目标中
  • 你可以使用Build Settings来使用不同的设置编译这两个目标。

例如,您可以为一个目标定义预编译器macros,为另一个目标定义其他macros(假设OTHER_C_FLAGS = -DPREMIUM在目标“PremiumVersion”中, OTHER_C_FLAGS = -DLITE在“LiteVersion”目标中定义LITEmacros),然后包括类似的代码在你的来源:

 -(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 { ... }