views:

522

answers:

4

Hi, I'm writing a Cocoa app in Objective C that's communicating with a webservice and I want it to connect to a sandbox in debug mode and to the real webservice in release mode. All I need is to change on line of code where an object that holds the configuration gets instantiated (with a different init-message and different parameters).

So how would I swap a line of code for Release or Debug mode?

+1  A: 

First, define a preprocessor symbol that is only set in your Debug build configuration, as per the question 367368 - call it, say, DEBUG. Then you can do

#ifdef DEBUG
  // Code that only compiles in debug configuration
#else
  // Code that compiles in other configurations (i.e. release)
#endif
Adam Wright
+5  A: 

You could check for #ifdef DEBUG, but I would recommend you don't do that.

There are a lots of differences between Debug and Release builds. Different compiler optimizations, different sets of symbols, etc...

Invariably, you are going to find yourself in a situation where you want to run the Release build against your sandbox for debugging purposes.... and your debug build against the production webservice because some customer has a problem that only reproduces in Release mode.

So, for that, I'd suggest a user default. See NSUserDefaults.

Note that simple user defaults can be set from the command line.

Thus, you could do something like:

/path/to/Myapp.app/Contents/Macos/Myapp -ServerMode Debug
bbum
Interesting technique, I'll have to remember that.
pix0r
Thanks for this tip! Your terminal command didn't really work for me. I found "defaults write my.bundle.identifier SandboxModeFlag -bool YES" to be working fine for me.
Christian
+5  A: 

You can use config-specific defines to change the code that's executed. Read about how to define a preprocessor symbol in Xcode first. Then, in your code, do something like this:

#if DEBUG_MODE
#define BACKEND_URL @"http://testing.myserver.com"
#else
#define BACKEND_URL @"http://live.myserver.com"
#end

NSURLRequest *myRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:BACKEND_URL]];
pix0r
Although I prefered bbum's solution, I now understand how this works and will keep it in mind. Thanks.
Christian
A: 

How to put build variables in build info for iPhone apps dev?

SKG
You should ask this as a separate question, not as an answer on someone else's question.
Peter Hosey