Where to set environmental variables in iOS?

后端 未结 3 2089
旧巷少年郎
旧巷少年郎 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:24

    You can also set the different URLs in your plist file by adding new String rows if you don't want to use a constants file.

    (Not much to be gained here though I admit but depends on where you prefer to set up your urls)

    In your myApp-plist file add new rows like this:

    "DevelopmentEndpoint" "String" "http://development.endpoint.com"

    "ProductionEndpoint" "String" "http://production.endpoint.com"

    In your code:

    // Get endpoint from main bundle
    #ifdef DEVELOPMENT
        NSString *endpoint = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"DevelopmentEndpoint"];
    #else
        NSString *endpoint = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"ProductionEndpoint"];
    #endif
    

    In your build settings add DEVELOPMENT under the "Preprocessor Macros" section for Debug or Release etc.

提交回复
热议问题