barakbd
barakbd

Reputation: 1102

Difference between Xcode build settings and Scheme environment variables

What is the difference between setting environment variables in Build Stting>User Defined vs Edit Scheme>Arguments>Environment Variables? I have a Debug Staging Configuration and using it in a scheme. Would like to use these variables in info.plist and inside my Swift code. (setting URL endpoints, api keys etc.) to switch between environments.

Upvotes: 4

Views: 6709

Answers (2)

epaus
epaus

Reputation: 121

You can access the environment variables by:

Swift:

  let environment = ProcessInfo.processInfo.environment
    if let environmentValueString = environment["VARIABLE_NAME"] {
        environmentValue.text = environmentValueString
    }

Objective C:

    NSDictionary *environment = [[NSProcessInfo processInfo] environment];
    if (environment[@"server_url"]) {
// Set server url with the value in the         environment
    } else {
       // Set the default one
    }

Upvotes: 5

Gary Makin
Gary Makin

Reputation: 3169

Build Setting are used when building. The Scheme > Run > Arguments > Environment Variables are used at run time.

If you want to use variables in your .plist file, you'll need to have them available when building, so Build Settings is where you'll define them.

Upvotes: 10

Related Questions