I have 2 API keys that are currently used in my code, and are also hardcoded inside the code. These keys need to be removed from the code completely and added in as environment variables instead from outside Xcode during the build phase. So far I have tried having this script
export API_KEY=keyvalue
as a Run Script that occurs before Compile Sources in the build phase, having that script as a Pre Action in the project's scheme, and also building the app from the command line with this but the environment variables for the given keys are always null.
xcodebuild -project MyProject.xcodeproj \
-scheme "My Scheme" \
-sdk iphoneos \
-destination 'platform=iOS,name=<devicename>' \
-derivedDataPath './output' \
API_KEY='KEY' \
SECOND_API_KEY='KEY' \
When manually building the project like this I can see the keys being exported correctly like all the other settings but cannot seem to access them with NSProcessInfo.processInfo.environment objectForKey:@"API_KEY" or with NSUserDefaults. I have also tried editing the scheme in Xcode so that the environment variables are set as API_KEY = $(API_KEY) along with the above but that did not work, as well as setting default values for these keys in Build Settings which also did not work. Is this actually possible to do? Thanks.
NSProcessInfo. - sboothNSProcessInfo.processInfo.environment. Generally you would set environment variables in Xcode with Edit Scheme > Run >Environment Variables but then the API keys would be in the code still, so they need to be set manually with a script outside the app when the app is built. Something similar to this, except this was for testing: stackoverflow.com/questions/23284829/… - Versicarius