7

I am developing an iPhone app which uses a server somewhere to fetch its data. Somewhere in the app's source code I hardcoded the URL to use to connect to. This is fine, except that I don't always want to test using a production server! I don't want to mess with live data, just to test something locally. So I set up a local version of that same server. But in order to make the iPhone app use that server is to change the hardcoded URL in the source code.

This is a little bit of a pain in the ass to do if you're often switching between the two servers. Also, I might accidentally release the app which still uses the local URL!

I was thinking that maybe XCode can help me with this since it has the notion of a "Debug" and a "Release" configuration option to build with. So my question is: can I somehow change the Debug configuration in a way that it points to local server URL? Maybe through pointing to a properties or plist file which contains the environment specific URL. I could then make two versions of this properties file and make the debug configuration point to one, while make the release configuration point to the other.

Does anyone know how I can accomplish this?

3 Answers 3

5

Put this code where you need to use the configuration based on the mode (debug/release) = (development/production).

The best place to put it is on the "ProjectName"_Prefix.pch file.

#ifndef __OPTIMIZE__ // __OPTIMIZE__ is not enabled, it means that the active config is Debug, so here you have to put your code for development mode

// For example
#define SERVER_URL @"http://my.test.server/something"

#else //__OPTIMIZE__ is defined, so put here your production code

// For example
#define SERVER_URL @"http://my.production.server/something"

#endif // __OPTIMIZE__

Cheers,
VFN

2

In one of your header files (such as the pre-compiled header file) define macros with the URL. Take a look at this article and use a similar approach.

Incidentally, I'm using the logging approach from this article in all my apps - it works like a charm, I strongly recommend it!

2
  • This approach is a little bit tricky. As you can check on the comments of the post for the article, some people had problem with it. And also you need to define compilation flags, when you can default flag/identifiers that the iPhone SDK/GCC already implement ('OPTIMIZE', 'DEBUG', or whatever)
    – vfn
    Feb 2, 2010 at 23:16
  • I got it to work - but I guess because you have to set this up for each project you start with, it'll be a hassle in the long and possibly even short runs. This should be setup in the XCode or compiler environment IMO.
    – Jonny
    Sep 8, 2010 at 9:26
1

You can define pre-proccessor macros in xcode by simply editing the gcc language settings:

Go to the Project menu and select "Edit Project Settings". Go to the "Build" tab .

Go to the section labeled "GCC 4.0 - Language". There is a setting named "Other C Flags". Add all the "-Dwhatever" macros you want there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.