ios-nd-networking
ios-nd-networking copied to clipboard
Use an alternative for storing API key in TMDB Client.
When first committing the Movie Manager code, I accidentally left my API key in the TMDBClient.swift file. Oops! This is a common mistake, especially when the API key is in the same file that's being edited frequently.
We shouldn't leave our API key in version control, and there are a variety of (non-perfect) ways to address this.
- Store it in a separate file (Plist, JSON, etc.) that's stored on the device.
- Move its constant to a Swift file that's ignored by version control.
And there are others. But since we want to keep the key private to ourselves, it's definitely not a good idea to store it in TMDBClient.swift
, and we should try to find a better place to put it.
https://github.com/sean-williams24/The-Movie-Manager.git
Created a plist for API Key. ☺️
Step 1: Create a plist file name "Secret.plist" Step 2: Add API key to plist file as API_KEY = [YOUR_API_KEY] Step 3: Add "Secret.plist" to git ignore
We might as well store our api key to the environment variables.
Step 1: Open Xcode's scheme editor.
Step 2: Select Run/Arguments in the editor.
Step 3: Add apiKey entry for the Name, and your api key string for the Value.
Step 4: In the TMDBClient, remove your apiKey static variable, and in the Endpoints enum change apiKeyParam to this:
static let apiKeyParam = "?api_key=\(ProcessInfo.processInfo.environment["apiKey", default: ""])"
.