How I Switch between Staging and Production URLs

How do you switch your app between a staging URL (for development and testing) and a production URL (for real world use)? I’ve changed my mind about my approach, because one size doesn’t fit all.

In 9 Code Smells of the Preprocessor (and Their Fixes), I originally suggested that instead of using the preprocessor, it would be better to use a plist. Here’s the preprocessor code smell:

#if STAGING
static NSString *const fooServiceURL = @"https://dev.foo.com/services/fooservice";
#else
static NSString *const fooServiceURL = @"https://foo.com/services/fooservice";
#endif

I wrote, “Instead of defining these URLs in your code, treat them as resource definitions and place them in a plist, organized by type.”

<dict>
    <key>Staging</key>
    <dict>
        <key>fooServiceURL</key>
        <string>https://dev.foo.com/services/fooservice</string>
    </dict>
    <key>Production</key>
    <dict>
        <key>fooServiceURL</key>
        <string>https://foo.com/services/fooservice</string>
    </dict>
</dict>

I still think this is a good approach for a complicated app that talks to many services. Putting all the URLs in a plist makes them easy to find and manage.

But for a simple app, I’ve found this to be overkill. Instead, I create a class for the URLs, and access them through methods:

- (NSString *)fooURLString
{
    DebugSettings *debugSettings = [self debugSettings];
    if ([debugSettings usingStaging])
        return @"https://dev.foo.com/services/fooservice";
    else
        return @"https://foo.com/services/fooservice";
}

The plist approach also makes it harder to switch between staging and production on the fly, because all the URLs have to be reloaded. With the “define it in code” approach, all I have to do to switch URLs is change a property in DebugSettings.

Question: What are your approaches switching your app between a staging URL and a production URL? Leave a comment below.

Did you find this useful? Subscribe today to get regular posts on clean iOS code.

About the Author Jon Reid

Jon is a coach and consultant on iOS Clean Code (Test Driven Development, unit testing, refactoring, design). He's been practicing TDD since 2001. You can learn more about his background, or see what services he can bring to your organization.

follow me on:

Leave a Comment:

16 comments
Add Your Reply