Search code examples
iosswiftvisual-studio-app-center

Using Microsoft App Center environment variables in iOS


I want to open source my iOS app but still be able to build it using a CI system like Microsoft App Center. My app uses private API keys that I want to hide from my open source repository.

Fortunately App Center offers the environment variables feature, which are intended to use at build time. Their documentation says they can be used like this in bash:

$ENVIRONMENT_VARIABLE

So, say I have an environment variable called GoogleAPIKey configured in App Center, can I retrieve it like this ProcessInfo.processInfo.environment["GoogleAPIKey"] ?? "" in my app? If yes, how do I test the app on my device if only App Center has the key (I don't want to put it in my code since that would mean it would be public (do I need to use gitignore)?


Solution

  • I don't think you can read environment variables inside the app. You can access environment variables in a custom bash scripts only. However I think you can modify app project files inside the build script. So you may try something like this:

    1. Add a special text file to xcode project and commit it to your repository.
    2. Add environment variable with GoogleAPIKey value
    3. Write GoogleAPIKey value to the text file inside the build script.
    4. Read GoogleAPIKey value from text file with your Swift code