I have different environments in my project (named from 1 to 9) and they are set in info.plist "Environment" key.
How can I access those environments in code to change a variable based on the environment the archive was generated from?
For example, supposing I generated an archive on environment n. 9, I want to use the URL "https://example2.com". The bolded part is the one I can't figure out (supposing the rest is correct too):
func webViewURLRequest() -> URLRequest {
if **ENVIRONMENT = 1** {
let url = URL.safe(stringURL: "https://example1.com")
}
else {
let url = URL.safe(stringURL: "https://example2.com")
}
return URLRequest(url: url)
}
Second attempt based on Leo's answer:
func webViewURLRequest() -> URLRequest {
let environment = Bundle.main.infoDictionary?["Environment"] as? String
if environment == "1" {
let url = URL.safe(stringURL: "https://example1.com")
} else {
let url = URL.safe(stringURL: "https://example2.com")
}
return URLRequest(url: url)
}
CodePudding user response:
You can use Bundle infoDictionary and get your environment value from there:
Bundle.main.infoDictionary?["CFBundleIdentifier"]
CodePudding user response:
In your second attempt url can't be found in scope because you create it inside of an if statement. Assuming your environment is properly found, you can rearrange your code to something like this just to test it:
func webViewURLRequest() -> URLRequest {
let environment = Bundle.main.infoDictionary?["Environment"] as? String
if environment == "1" {
return URLRequest(url: URL.safe(stringURL: "https://example1.com"))
} else {
return URLRequest(url: URL.safe(stringURL: "https://example2.com"))
}
}
