Where to set environmental variables in iOS?

后端 未结 3 2090
旧巷少年郎
旧巷少年郎 2020-12-28 11:02

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 i

3条回答
  •  独厮守ぢ
    2020-12-28 11:41

    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 
        #import 
        #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!

提交回复
热议问题