How do you read environment variables in Swift?

← Swift answers

Read them with ProcessInfo.processInfo.environment["NAME"]. For development, set them in the Xcode scheme; a shipped iOS app does not inherit your shell's variables.

let env = ProcessInfo.processInfo.environment
 
let home = env["HOME"]                 // String?
let isCI = env["CI"] != nil
 
if let level = env["LOG_LEVEL"] { print(level) }
  • In Xcode: Product > Scheme > Edit Scheme > Run > Arguments > Environment Variables adds values for debug runs only.
  • On a device outside Xcode, the variables you set in the scheme are not present, so never rely on them in a release build.
  • For values that must ship, such as an API base URL per configuration, define them in an .xcconfig file, expose them through Info.plist and read them with Bundle.main.object(forInfoDictionaryKey:).
  • Do not ship secrets this way; anything in the app bundle can be extracted.

Related questions

Start debugging smarter today

Try Bugsee for free — no commitments, no hidden fees.

Free sign upLive demoNo credit card required