Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode Objective C macros and build settings, switching dev and staging and production--a full example?

Tags:

ios

build

I have an ios application that makes RESTful requests. I want to automatically change up my service calls endpoints depending on whether I am sending web service RESTful requests to a. development server b. staging server c. production server

For example,

a. dev url: http://www.dev.camerakingphotos/getAllPhotos
b. test url: http://www.test.camerakingphotos/getAllPhotos
c. production url: http://www.camerkingphotos/getAllPhotos
  1. How do I tell XCode to switch URLs to my production server vs development server effortlessly? Is it a build setting, a preprocessor macro, a scheme, or a new target (I think a new target is not what I want). Ideally XCode would know that right now I'm testing a development version of my app, so I want all of the URLs to point to the development server. And I want to tell XCode that I'm now ready to test my app to point to production, so use the URLs that correspond to the production server.

  2. If I define a preprocessor macro, where would I do so? I have an APP_CONSTANTS.h file that is the single place where all my service URLs are stored. Do I define a preprocessor macro in my APP_CONSTANTS.h file or in a Prefix.pch file?

  3. Would anyone care to spell out a detailed example for me?

I've looked around stackoverflow but I would appreciate someone putting all the pieces together, explicitly for me, that does not assume too much prior knowledge. I perused the XCode build guide (most of it) but I saw the trees only and not the forest.

Thanks!

like image 279
user798719 Avatar asked Jul 19 '13 01:07

user798719


1 Answers

Xcode has (almost) nothing to do with this, if you want the compiler to know then you need to tell it. The compiler only knows what you tell it. I also have this setup. I use preprocessor macros to switch between them. Xcode, by default, defines a DEBUG preprocessor flag that is set when you make a development build of your app. It is not defined in the Release scheme. This is the easiest way to switch between the two servers:

.h file

extern NSString *const MYServerURL;

.m file

#ifdef DEBUG
NSString *const MYServerURL = @"http://www.dev.camerakingphotos/getAllPhotos";
#else
NSString *const MYServerURL = @"http://www.camerakingphotos/getAllPhotos";
#endif

Note that I didn't #define these. This has the bonus that when you change the .m file, only it needs to be recompiled (as opposed to the former which requires every file that included the header to be recompiled). These variables will be available globally to any file that included the header where the variable is defined. If you need more finegrained control, you can define your own preprocessors items in your .pch file and turn them on and off as needed (via commenting). Then just use the above pattern and instead of DEBUG use your items (However, don't forget to switch them to the appropriate value before you release).

like image 133
borrrden Avatar answered Oct 25 '22 00:10

borrrden