← Back to list

AppConfigs in iOS: Injecting Constants for Different Configurations in Xcode

When building iOS apps, you often need to manage different environments — such as Development, Staging, and Production. Each environment…

Dmytro Shapovalov · 2025-09-29 09:26 · 0 claps · 1.5 min read
#appconfig #swift #xcode #configuration-file #ci-cd-pipeline
Open on Medium ↗
Wiki topics: 📱 · Mobile Development ☁️ · DevOps & Cloud

AppConfigs in iOS: Injecting Constants for Different Configurations in Xcode

When building iOS apps, you often need to manage different environments such as Development, Staging, and Production. Each environment might require its own API endpoints, associated domains, feature flags, or analytics keys.

Instead of sprinkling #if DEBUG Or hardcoded constants across the codebase, we can use Xcode Configurations and xcconfig files to inject constants cleanly and maintainably.

Why AppConfigs?

Imagine you have:

You don’t want to switch them manually before every build. Instead, you want Xcode to pick the correct values based on the selected build configuration.

Step 1: Add Configurations in Xcode

By default, Xcode provides two configurations: Debug and Release. To support multiple environments:

  1. Open your project in Xcode.
  2. Go to Project Settings → Info tab.
  3. Duplicate Debug and Release to create:
  • Debug-Development
  • Debug-Staging
  • Release-Production

Step 2: Create .xcconfig Files

Create .xcconfig files for each configuration in your project root:

AppConfig/
 ├── Development.xcconfig
 ├── Staging.xcconfig
 └── Production.xcconfig

Example: Development.xcconfig

BASE_URL = https://dev.api.myapp.com
API_KEY = dev-12345

Example: Staging.xcconfig

BASE_URL = https://staging.api.myapp.com
API_KEY = staging-67890

Example: Production.xcconfig

BASE_URL = https://api.myapp.com
API_KEY = prod-abcde

Step 3: Link Config Files to Configurations

  1. Select your project in Xcode.
  2. Go to Info → Configurations.
  3. Assign each .xcconfig file to the correct configuration.

Example:

  • Debug-DevelopmentDevelopment.xcconfig
  • Debug-StagingStaging.xcconfig
  • Release-ProductionProduction.xcconfig

Step 4: Access Constants in Code

The values defined in .xcconfig files are available via Info.plist if we inject them properly.

Add placeholders in your Info.plist:

<key>BASE_URL</key>
<string>$(BASE_URL)</string>
<key>API_KEY</key>
<string>$(API_KEY)</string>

Now, read them in Swift:

enum AppConfig {
    static var baseURL: String {
        Bundle.main.object(forInfoDictionaryKey: "BASE_URL") as? String ?? ""
    }

    static var apiKey: String {
        Bundle.main.object(forInfoDictionaryKey: "API_KEY") as? String ?? ""
    }
}

Usage:

print("API Base URL: \(AppConfig.baseURL)")
print("API Key: \(AppConfig.apiKey)")

Step 5: Create Schemes for Easy Switching

Finally, create Schemes for each environment:

  • MyApp-Dev
  • MyApp-Staging
  • MyApp-Production

Assign the correct Build Configuration to each Scheme. Now, switching environments is just a matter of selecting a scheme.

Conclusion

By leveraging AppConfigs with xcconfig files, you can:

✅ Cleanly separate constants per environment ✅ Avoid hardcoding secrets and URLs in code ✅ Easily switch between environments with schemes ✅ Keep build metadata handy

This setup keeps your project scalable, clean, and production-ready.


메타데이터
post_id
0af359b2c94a
slug
appconfigs-in-ios-injecting-constants-for-different-configurations-in-xcode-0af359b2c94a
url
https://medium.com/@dmytroshapovalov/appconfigs-in-ios-injecting-constants-for-different-configurations-in-xcode-0af359b2c94a
canonical_url
https://medium.com/@dmytroshapovalov/appconfigs-in-ios-injecting-constants-for-different-configurations-in-xcode-0af359b2c94a
author_url
https://medium.com/@dmytroshapovalov
status
ok
fetched_at
2026-07-08 10:57:59