An iOS app cannot keep a secret that ships inside it: anything in the app bundle, Info.plist or an .xcconfig-generated value can be extracted. Keep third-party secret keys on a server your app calls. Store per-user tokens the app receives at run time in the Keychain, which Apple provides for small secrets such as passwords and tokens.
Read the key from the environment
import Security
import Foundation
func readToken(account: String) -> String? {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: "com.example.app",
kSecAttrAccount as String: account,
kSecReturnData as String: true,
kSecMatchLimit as String: kSecMatchLimitOne
]
var item: CFTypeRef?
guard SecItemCopyMatching(query as CFDictionary, &item) == errSecSuccess,
let data = item as? Data else { return nil }
return String(data: data, encoding: .utf8)
}Local development
Build settings in .xcconfig files can inject values into Info.plist, which is convenient for non-secret config such as an environment name. Keep any file with real keys git-ignored, but remember anything read from the bundle at run time is in the shipped app. For development, keep test-mode keys and use a local or staging backend.
Production
Authenticate users to your own backend (for example with Sign in with Apple or OAuth with PKCE) and store the resulting short-lived tokens with SecItemAdd using an accessibility class such as kSecAttrAccessibleWhenUnlockedThisDeviceOnly. Keep powerful third-party credentials server-side and expose narrow endpoints. Use App Attest or similar checks if you need to limit calls to genuine app instances.
Mistakes to avoid
- Hardcoding an API key in Swift source, a plist or an xcconfig: it ships in the IPA and can be extracted with standard tools.
- Storing tokens in
UserDefaults, which is plaintext in the app container and included in backups, rather than in the Keychain. - Using Keychain items with an overly permissive accessibility class such as
kSecAttrAccessibleAlways(deprecated) for sensitive tokens. - Logging tokens or request headers with
printor network debugging tools left on in release builds.
When an AI agent works on your iOS (Swift) project
Coding agents run inside your project folder, so they can read the same .env file your app reads. BotPassword injects keys at run time instead: the agent calls the run_with intent through the MCP server and the process gets the variables it needs while the agent sees only redacted output. Planned: bpw run, a CLI form of the same.
Frequently asked questions
Where should I store an API key in an iOS app?
Do not store a secret API key in the app at all; keep it on your server. Tokens the app receives for a signed-in user belong in the Keychain.
Is Info.plist a safe place for secrets?
No. Info.plist is part of the app bundle, which anyone with the app can unpack and read.
Should I keep API keys in a .env file for iOS (Swift)?
A .env file that is git-ignored is fine for local development, but every process and every AI coding agent running in that folder can read it. Load secrets into the environment at run time from a password manager or secrets manager instead, so no plaintext file sits in the project.