Changing the base url depending on the value of the preprocessor macro

I have a project with several schemes, one for EA, Staged and Production.

I want to be able to set the base url based on the configuration of the assembly that I am running.

#if defined PRODUCTION #define BASE_URL [NSURL URLWithString:@"https://example.production.com/"] #elif defined STAGED #define BASE_URL [NSURL URLWithString:@"http://example.staged.com/"] #else #define BASE_URL [NSURL URLWithString:@"https://example.ea.com/"] #endif 

Is there a way to set preprocessor macros to determine the PRODUCTION and STAGED values, I assume this is somewhere in the build settings of my target. And will this be the best way to do this?

+1
source share
3 answers

I usually store my URLs in an NSObject class (Aptly named URLHub) with these class methods;

 +(NSString *)Login { NSString *url; if (developmentMode) { url = @"https://dev.mycoolwebservice/api/login"; } else { url = @"https://mycoolwebservice/api/login"; } return url; } 

Then wherever I need to use this url, I can easily grab it;

 #import "URLHub.h" NSString *url = [URLHub Login]; 

This approach makes it easy to update URLs throughout the application, as they are all stored in one place.

In this example, to go to the development URLs, I just need to flip one BOOL and all the changes in the url app .;)

+1
source

There are several ways to achieve functionality, although I only know what works with circuits.

The first option goes beyond the scope of the scheme and works with target macros of the preprocessor - for development, DEBUG is set to 1 and produces, DEBUG is set to 0. You can add as many macros as you want - to do this, go to your goals "Build Settings" and find "Macros preprocessor "is almost everything.

To work with schemes, you need to add an environment variable to your scheme:

Edit Schema -> Add Environment Variable.

To access the environment variable that you will need to execute:

 [[[NSProcessInfo processInfo] environment] objectForKey:@"myKey"] 
+2
source

In the "Other C Flags" assembly setup, put -DPRODUCTION and -DSTAGED into different build configurations. (Of course, you will need to create these additional build configurations.)

+1
source

Source: https://habr.com/ru/post/945013/


All Articles