Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to set environmental variables in iOS?

In my development I wish to hit a development URL when im testing but when the app is run on my iPhone, I want it to run on production URL. How do I get about setting this in Xcode?

Almost seems like I need to be able to set environmental variables (which in this case are different URLs).

Where do I set these up? Please hand hold me on the answer as I am fairly new

Thanks

like image 511
jini Avatar asked Jul 20 '12 17:07

jini


1 Answers

There are lots of ways to do this. My preferred method is to create a file called Constants.h that looks like this:

//
//  Constants.h
//

#ifndef Constants_h
#define Constants_h

#pragma mark - Instances
#ifdef DEVELOPMENT
#define ROOT_URL @"http://127.0.0.1:8000"
#endif

#ifdef STAGING
#define ROOT_URL @"http://staging.example.com"
#endif

#ifdef PRODUCTION
#define ROOT_URL @"http://production.example.com"
#endif

Then, I just include it in my *-Prefix.pch file like this:

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "Constants.h"
#endif

In your build settings, set DEVELOPMENT, or STAGING, or PRODUCTION = 1.

Now you can access things like ROOT_URL anywhere in your project.

Good luck!

like image 125
Erik Avatar answered Oct 16 '22 23:10

Erik