If the sole purpose is to set different values for different environments - development, QA, UAT, Production
, the xcconfig
files can be used.
No conditional statements would be required but based on the configuration, it will take the appropriate values.
Steps involved would be:
Add different Schema and different build Configurations.
Add Configuration Settings file by selecting File->New->File and "Configuration Settings file"
-> Development.xcconfig, UAT.xcconfig, Production.xcconfig
etc

Add custom keys of your choice in the config file and their relevant values in Key-Value pair as:
endpointUrl = "Production_Url"
for Production.xcconfig
endpointUrl = "UAT_Url"
for UAT.xcconfig

- Add relevant plist files and mention the keys from .xcconfig file to the .plist file. Create UAT.plist for UAT.xcconfig file and similar way for other supported configs.

- Set the appropriate plist path for each build configuration under
Project -> Info -> Configurations

- Create a .swift file to read the config file such as:
public enum PlistKey {
case EndpointURL
func value() -> String {
switch self {
case .EndpointURL:
return "endpointUrl"
}
} }
public struct Environment {
fileprivate var infoDict: [String: Any] {
get {
if let dict = Bundle.main.infoDictionary {
return dict
}else {
fatalError("Plist file not found")
}
}
}
public func configuration(_ key: PlistKey) -> String {
switch key {
case .EndpointURL:
return infoDict[PlistKey.EndpointURL.value()] as? String ?? "noValue"
}
}
}
- Call the url as:
let server_url = Environment().configuration(PlistKey.EndpointURL)
More detailed version at: https://www.freecodecamp.org/news/managing-different-environments-and-configurations-for-ios-projects-7970327dd9c9/