One of the most annoying drawbacks of developing desktop and mobile apps is the inability to switch between dev and production environments. When a client tells us about a problem with the app, our developers can’t reproduce the problem using the same app that is used by the customer.
Fortunately, we found a neat solution. We hard-code dev & production servers’ addresses inside the app that is released to customers. Then, depending on the platform, we use one of the tricks that tells the app to connect to the dev environment.
Choosing the environment on Mac OS X
Our Mac application picks the environment based on the app’s name. By default, our “LiveChat” app connects to the production server. But when we change the name to “LiveChat dev”, it connects to the development server.
Opening an app that connects to dev or production environment is just a matter of seconds. The source code of both apps is identical, so there’s no room for error.
Here’s the code that picks the environment in our Mac app:
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *appName = [[NSFileManager defaultManager] displayNameAtPath: bundlePath];
self.address = [[NSBundle mainBundle] infoDictionary][DefaultAppUrlKey];
if ([appName rangeOfString:@"dev" options:NSCaseInsensitiveSearch].location != NSNotFound) {
self.address = DevAppUrl;
}
Choosing the environment on Windows
Windows doesn’t come with a handy search as Mac does, so we determine the apps’ environment by passing a parameter in a shortcut that opens the app. We put two shortcuts (“LiveChat” and “LiveChat dev”) on the desktop. The “LiveChat dev” shortcut defines the “dev” parameter when opening the app.
Here’s the code that reads the flag and changes the environment accordingly:
protected override void OnStartup(StartupEventArgs e)
{
string appUrl = Constants.DefaultAppUrl;
if (e.Args.Count() > 0)
{
if (e.Args[0].CompareTo("dev") == 0)
{
appUrl = Constants.DevAppUrl;
}
}
[...]
}
Choosing the environment on iOS / Android apps
Connecting to the dev environment using the app already published in the App Store or Google Play may sound tricky. Adding the “Choose app’s server” option to the login screen is not a good solution because our customers shouldn’t know about other environments.
We came up with an idea of entering the “dev” phrase — followed by an empty space — right in the login field.
And here’s the code that we use in our mobile apps:
// iOS app
if ([login hasPrefix:@"dev "] ) {
appDelegate.useDevServer = YES;
login = [login substringFromIndex:4];
}
// Android app
boolean useDevServer = false;
if (login.startsWith("dev ")) {
useDevServer = true;
login = login.replace("dev ", "");
}
This solution lets us reproduce the problem on a dev environment using the app released to the App Store or Google Play.
The above examples remind us that simple ideas are often the best ones.