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…
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:
- Development API:
[https://dev.api.myapp.com](https://dev.api.myapp.com) - Staging API:
[https://staging.api.myapp.com](https://staging.api.myapp.com) - Production API:
[https://api.myapp.com](https://api.myapp.com)
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:
- Open your project in Xcode.
- Go to Project Settings → Info tab.
- Duplicate
DebugandReleaseto create:
Debug-DevelopmentDebug-StagingRelease-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
- Select your project in Xcode.
- Go to Info → Configurations.
- Assign each
.xcconfigfile to the correct configuration.
Example:
Debug-Development→Development.xcconfigDebug-Staging→Staging.xcconfigRelease-Production→Production.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-DevMyApp-StagingMyApp-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