Lazar Nikolov
—In most of the cases we’ll need to store some sort of a value or secret within our app. Whether that’s an API key, or our backend’s URL. We might even want to provide different values for certain properties in Development mode and Production mode.
Environment Variables are key-value properties that are defined in a certain place, and can be accessed from within the app. We usually use environment variables to store the API URL for both development and production, we also store our API keys that authorize our app with the backend. One of the super powers of the environment variables is the fact that they are stored in a separate file (that can be git-ignored!). That’s why it’s recommended to store your secrets as environment variables, so you don’t leak them on GitHub.
To setup support for environment variables, we need to create a new Configuration Settings File
in our project. So let’s create a new group called Configs
in our project and a new debug.xcconfig
configuration file inside of it.
Let’s add a value:
API_URL=debug.sentry.io/api
Now we need to set this file as the Debug configuration file for our project. Head to the Project Settings, and in the Configurations section, expand the Debug item and select the “debug” option for your project:
To make our API_URL
variable available in our app, we also need to register it in our info.plist
file. To do that:
Add Row
API_URL
and the value to $(API_URL)
This step injects our environment variables into the “Info Dictionary”, and now we can access them in our code:
if let apiUrl = Bundle.main.infoDictionary?["API_URL"] as? String { // we have our API_URL value! }
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.