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?
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!
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
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.
来源:https://stackoverflow.com/questions/2063922/iphone-switching-between-local-and-production-environment-settings