views:

501

answers:

3

Hello,

I want to have a constant in my project to change between Lite and Pro version. I don't think it is the best way to do it, but I am trying to:

  1. add a constant in my app delegate

    #define BUILD_PRO 1 //0 => LITE, 1 => PRO
    
  2. when I need it I import the appDelegate and test it:

    #import "myAppDelegate.h"
    

    then later

    #if (BUILD_PRO==1)
    NSLog(@"this is pro version");
    #endif
    

The problem is that this code works in some files and don't works in others. I haven't found any explanation for this behaviour; does anyone have an explanation for it?

And what is the right way to have two versions (pro and lite) from the same project? Thanks in advance

+2  A: 

One way of doing this is to have a target for the pro version and a target for the light version. Then you declare your constants in the build settings under Preprocessor Macros of the pro version.

Then in your code you can do:

#ifdef BUILD_PRO
   //super awesome pro code here.
#endif
Elfred
+6  A: 

Yep. A pre-processor definition is the way to do it.

I imagine it is working in some files and not others because some might not be including your myAppDelegate.h file and therefore not getting the definition. I suggest defining a "Lite Version" and "Pro Version" target and setting the pre-processor variable in the build configuration for each target.

Once you have created a lite target (just select the duplicate target context menu item on your "Pro Version" target to create the lite one):

  • Go into the Project/Edit Target "Pro Version" menu item
  • Go to the build tab and find the Preprocessing section (towards the bottom).
  • add BUILD_PRO=1 to the "Preprocessing Macros" section.

That way you don't have to change any header files, you just need to build either the lite or full target. If you need to add pro functionality anywhere in your product just use:

#ifdef BUILD_PRO
// do some pro stuff
#endif
Cannonade
+1 this is the best answer since it's defined in the build configuration which is the Xcode equivalent of doing it in a Makefile which is how it would be done properly with things using 'make'. Note that people usually just use "#define FOO" then "#ifdef FOO" ... "#endif" not "#define FOO 1" and "#if(FOO==1)" though in terms of logic it shouldn't matter really.
Nimrod
Thanks, good answerbut I don't find the "Preprocessing Macros" sectionI am following this tutorial http://just2us.com/2009/07/tutorial-creating-multiple-targets-for-xcode-iphone-projects/I've put "all configuration" and choosed the base SDK, as mentioned in the tutorial but I don't see the "Preprocessing Macros" sectionAll I see is "Info.plist Other preprocessor Flags", "Info.plist preprocessor Definitions", "Info.plist preprocessor Prefix File"(I am using iphone_sdk 3.1.2 with xcode 3.1.4)
someone0
It's okI found the problem: I must choose the device, not the simulator to find the macro preprocessor sectionNow it s woriking perfectly
someone0
A: 

I declare a variable in the header of AppDelegate.m:

int DEVICE_TYPE;

And then in applicationDidFinishLaunching in my AppDelegate, I call:

- (void) setDeviceType {

  NSString* machineType = [[UIDevice currentDevice] machine];
  if ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"]) {
    DEVICE_TYPE = IPOD_TOUCH;
  } else if ([machineType isEqualToString:@"iPhone1,2"] || 
             [machineType isEqualToString:@"iPhone1,1"]) {
    DEVICE_TYPE = IPHONE3G;
  } else {
    DEVICE_TYPE = IPHONE3GS;
  }
}
Andrew Johnson