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?
1 Answer
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
{
...
}
3 Comments
Cyrille
If you're accustomed to it, think of targets as different makefiles. They have the same role.
Slee
This is huge, thank you. To get this to really work right for me I needed to do a copy target then update the target settings to use a different Info.plist for my bundle name ect...
AliSoftware
Note that if you further add files to your project, be careful to add the file to both targets and not only the active one: when adding the file, in the dialog that appears to ask if you want to copy files or not etc, you have a list of your targets and checkboxes in front of each one. (The checkboxes state is saved so that you will probably need to check this only once anyway)