Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create multiple apps from same shared code in Xcode?

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?

like image 749
Slee Avatar asked Sep 17 '11 13:09

Slee


People also ask

Can multiple people work on Xcode?

You can collaborate on Xcode or any other IDE as if you are in the same space.


1 Answers

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:

  • 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:

-(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
{
   ...
}
like image 87
AliSoftware Avatar answered Oct 12 '22 21:10

AliSoftware