Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在Xcode中使用相同的共享代码创建多个应用程序?_Iphone_Ios_Xcode_Ipad - Fatal编程技术网

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

Iphone 如何在Xcode中使用相同的共享代码创建多个应用程序?,iphone,ios,xcode,ipad,Iphone,Ios,Xcode,Ipad,我正在开发两个不同的应用程序,它们共享95%的相同代码和视图。使用Xcode的最佳方法是什么?使用目标。这正是他们的目的 通常,大多数项目都有一个目标,对应于一个产品/应用程序。如果定义多个目标,则可以: 在两个目标中都包含一些源代码文件(或者全部),一些在一个目标中,另一些在另一个目标中 您可以使用构建设置来使用不同的设置编译两个目标 例如,您可以为一个目标定义预编译器宏,为另一个目标定义其他宏(比如在目标“PremiumVersion”中定义other___标志=-DPREMIUM,

我正在开发两个不同的应用程序,它们共享95%的相同代码和视图。使用Xcode的最佳方法是什么?

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

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

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

例如,您可以为一个目标定义预编译器宏,为另一个目标定义其他宏(比如在目标“PremiumVersion”中定义
other___标志=-DPREMIUM
,在“LiteVersion”目标中定义
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
{
   ...
}

如果您已经习惯了,可以将目标视为不同的makefile。他们有同样的角色。这是巨大的,谢谢。为了让它真正适合我,我需要执行一个复制目标,然后更新目标设置,以使用不同的Info.plist作为我的捆绑包名称等…请注意,如果您进一步向项目添加文件,请小心将文件添加到两个目标,而不仅仅是活动目标:添加文件时,在询问是否要复制文件等的对话框中,每个目标前面都有一个目标列表和复选框。(复选框状态已保存,因此您可能只需要选中一次)