How do I use environment variables at build time?

I have an app that relies on a REST like API and want to be able to set a default value for the server but also be able to override that value via environment variables at build time.

The idea is that by default it would make calls to localhost but when I run UI Tests on Xcode cloud I'd point it at a test server and then when I hit the phase to archive the binary I'd provide it with either the staging server or production server.

Of course the other question is is this the best way to do it or should I be using schemes instead, though that would code the addresses into the project and I like the ability to change the url outside of the project

  • Not sure of the exact requirements here but what about if the url is stored within a .plist file which is read at run time. The value in the .plist can be the actual final url, or it is easy to inject a different url into there at build time. There is a tool to edit the contents of plists.

Add a Comment

Replies

unless you can shift cfg into a restful endpoint, a typical 'side car' pattern - provide a collection of default cfgs in the app, add to startup code some reflection to identify which default cfg to use for that app session, and then develop setting view that override both current cfg and cfg data (for your sanity).