How to build a free version from a paid version without duplicating the Xcode 4 project?
Asked Answered
H

1

10

I've heard rumors that it is possible to build different variations of an app without duplicating Xcode projects, by using targets and conditional compilation instructions like

IF !FREE_VERSION 
[self loadGreatFeature];
ELSE
[self loadBoringFeature];

So:

  • How to set Xcode 4 up to be able to distinguish between building / archiving a free or paid version of the project?

  • How to tell Xcode 4 to include a certain set of images and other resources in the paid version, but not in the free version (and vice versa)?

  • How to tell Xcode 4 to build the free OR paid version? (don't want to build both of them all the time as this would slow development down)

  • What are the caveats of this approach?

I do know what's the caveat of duplicating the Xcode project: Once I fix a bug in either version, I must do the same thing in the other. Same goes for making improvements and modifications.

Hettie answered 10/12, 2011 at 12:13 Comment(0)
I
20

Open your project in Xcode and select the top-level item in the Project Navigator. You should see the list of targets for your project.

Create a new target for the free version of your app. An easy way to do this is to control-click a current target that's similar to what you want and duplicate it.

Select your new target. In the Build Phases tab you can control which source files will be built as part of this target and which resources are copied for it. In the Build Settings tab search for Preprocessor Macros and add a new one, for example MYAPPLITE=1, for all build configurations. You can then do conditional compilation in your code with something like:

#ifdef MYAPPLITE
    [self loadBoringFeature];
#else
    [self loadGreatFeature];
#endif

Finally, select Edit Scheme... from the Product menu. A new scheme should already have been created for your new target. You can rename it in the Manage Schemes sheet if you want. You can control specific settings for building, running, archiving etc. the new target here.

To switch between building the free version or the paid version you just change the active scheme.

The only real caveat is that you need to keep the settings for your new target up to date.

Insusceptible answered 10/12, 2011 at 13:37 Comment(8)
Why "=1" in "MYAPPLITE=1"? Wouldn't "MYAPPLITE" be enough?Hettie
Solution for "The only real caveat is that you need to keep the settings for your new target up to date.": Before I duplicated my target, I transferred all target-specific settings to the Project level. Now both targets inherit these settings from the Project.Hettie
Did the steps outlined above. However, when I build the Free version, the app immediately crashes with no log in the console. What can be the problem?Hettie
Yes, you could omit the "=1", I add a value out of habit. Regarding the crashing - initially the new target should be exactly the same as the original, does that target still build and run ok?Insusceptible
Yes it does. Figured out the crash happened because I didn't delete the previously installed app on the device. Now it works. Except for the conditional compilation. It does work now, but somehow Xcode 4 doesn't syntax-highlight any conditional code in the #else part...Hettie
@sjs: Could you write a small paragraph about handling different bundle IDs for the lite and full versions?Silicone
The preprocess works fine in my simulator but doesn't work in device. Why like this? ThanksRounder
Just in case anyone needs this :) raywenderlich.com/68613/create-paid-lite-version-iphone-appUppity

© 2022 - 2024 — McMap. All rights reserved.