问题
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
)?
回答1:
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:
- Add a special text file to xcode project and commit it to your repository.
- Add environment variable with GoogleAPIKey value
- Write GoogleAPIKey value to the text file inside the build script.
- Read GoogleAPIKey value from text file with your Swift code
来源:https://stackoverflow.com/questions/54266481/using-microsoft-app-center-environment-variables-in-ios