The short answer
- Store tokens and keys in the iOS Keychain, or encrypt them with a key held in the Android Keystore, and never in plain files or preferences.
- Treat anything shipped inside the app as public, and enforce every permission on the server, because an attacker can call your API without the app.
- Certificate pinning, root detection and app attestation add protection with trade-offs attached, and none of them replaces a secure API.
Mobile app security is the set of measures that protect an app’s data, its users’ accounts and the systems it talks to, on a device you do not control. The basics are to keep secrets out of the app, store tokens in the platform’s secure storage, use short-lived tokens, enforce every permission on the server and test against a recognised standard. Many serious weaknesses sit in the API behind an app, so security work starts there as much as on the phone.
Why the device cannot be trusted
An app runs on a phone that someone else owns. Anyone can download it, unpack it, read its code, watch its network traffic and run it on a modified device. Two rules follow from that.
- Anything shipped in the app is public. API keys, secrets and hidden URLs in the app bundle can be extracted. Keys that must stay secret, such as a payment provider’s secret key or an AI provider’s API key, belong on your server, and the app calls your server instead.
- The app is not the security boundary. Checks in the app improve the experience. Checks on the server are what protect the data.
Secure storage with the Keychain and Keystore
Each platform provides secure storage, and tokens, keys and other sensitive values belong there.
iOS Keychain
The Keychain is an encrypted store managed by the operating system. Items can be limited to the current device, so they are not copied to a new phone from a backup, and they can require a fresh Face ID or Touch ID check before they are read.
Android Keystore
The Android Keystore holds cryptographic keys in a way that makes them hard to extract, using secure hardware on many devices. Apps do not usually put tokens in the Keystore itself. They encrypt the tokens with a key held in the Keystore and store the encrypted result. Keys can also be set to require the person to authenticate before each use.
Plain storage, such as UserDefaults on iOS or SharedPreferences on Android, should only hold data you would not mind someone reading. The same thinking applies to the local database in an offline app. Estimark saves to the phone first so people can work without signal, and any app built that way has to decide what is stored on the device, for how long and how it is protected if the phone is lost. Hide sensitive screens from the app switcher, keep tokens and personal data out of logs, and exclude sensitive files from device backups.
Tokens and sessions
Most apps sign people in once and then use tokens. A short-lived access token is sent with each API request, and a longer-lived refresh token is used to get new access tokens.
- Keep access tokens short-lived, measured in minutes rather than days.
- Issue a new refresh token each time one is used, and revoke the whole chain if an old one is presented again, because that suggests it was stolen.
- Let people see and end their sessions on other devices, and end them all when the password changes.
- For sign-in through an external identity provider, use the system browser with OAuth 2.0 and PKCE, the approach recommended for native apps, instead of an embedded web view that could capture passwords.
- Revoke tokens on the server when someone signs out. Deleting them from the phone is not enough.
Certificate pinning and its trade-offs
Every connection should use HTTPS, and both platforms push apps that way by default. App Transport Security on iOS blocks insecure connections unless an exception is declared, and Android blocks plain HTTP by default for apps that target Android 9 or later.
Certificate pinning goes a step further. The app accepts only a specific public key or certificate for your server, which protects against interception by someone holding a certificate from a compromised or misused certificate authority. The risk is operational. If your certificate or hosting provider changes and the app was not prepared, every installed copy stops connecting until people update.
If you pin, pin public keys instead of whole certificates, include at least one backup key, and plan how keys will be rotated. iOS supports pinning in the app’s configuration through NSPinnedDomains, and Android supports it through its network security configuration, which can set an expiry date on pins.
Biometrics, jailbreaks and attestation
Biometrics
Face ID, Touch ID and Android’s biometric prompt let a person confirm it is them without typing a password. The app never sees the face or fingerprint, only a result. A simple yes or no can be bypassed on a modified device, so the stronger pattern ties the biometric check to a key in the Keychain or Keystore. The key, and the token it protects, can then be used only after a successful check.
Jailbroken and rooted devices
Jailbreaking an iPhone or rooting an Android phone removes protections the operating system provides. Apps can look for signs of it, but a determined attacker can always hide them. Treat detection as a signal to record, or a reason to limit high-risk actions, and never as a guarantee.
App attestation
Apple’s App Attest and Google’s Play Integrity API let your server check that a request comes from a genuine copy of your app on a genuine device. The server verifies a token signed by Apple or Google, which is much harder to fake than anything the app reports about itself. It helps against scripted abuse of your API, such as fake sign-ups and scraping.
Securing the API behind the app
An attacker does not need your app to call your API. They can replay its requests with any tool. One of the most damaging flaws is broken object-level authorisation, where changing an ID in a request returns someone else’s data, and OWASP’s API security guidance treats it as a leading risk.
- Check on the server, on every request, that the signed-in user may access that specific record, within their own organisation’s data.
- Validate all input on the server, whatever the app has already checked.
- Rate-limit sign-in, password reset and expensive endpoints.
- Return only the fields the screen needs, so a response never carries data the person should not see.
- Log security events, such as failed sign-ins and refused requests, without logging tokens or passwords.
Our guide to roles and permissions covers how to model who may do what.
What to test
OWASP publishes the Mobile Top 10 alongside the Mobile Application Security Verification Standard (MASVS) and a testing guide that explains how to check each requirement. Use them as a checklist, in proportion to the data your app holds.
| Area | How to test it | When |
|---|---|---|
| Data stored on the device | Inspect the app’s files, databases and backups on a test device | Before launch and after storage changes |
| Network traffic | Route the app through an intercepting proxy and read every request and response | Before launch and after major changes |
| API authorisation | Replay requests as a different user and change record IDs | Every release, with automated tests |
| Secrets in the app | Unpack the release build and search it for keys and URLs | Every release |
| Dependencies and SDKs | Scan for known vulnerabilities and review what each SDK collects | Continuously, in the build pipeline |
| Sessions | Sign out, change the password, revoke a device and reuse an old refresh token | Before launch and after sign-in changes |
| Modified devices | Run the app on a rooted or jailbroken test device | Before launch |
For apps that handle payments, health information, children’s data or large amounts of personal data, commission an independent penetration test before launch. UK GDPR requires appropriate technical and organisational measures to keep personal data secure, and a documented test helps show you have taken them. The checks larger customers ask for are covered in security before enterprise customers.
Questions people ask
Is it safe to put an API key in a mobile app?
Only if the key is meant to be public, such as a key restricted to your app that can only do harmless things. Any key that grants access to paid services or private data can be extracted from the app and should live on your server instead. The app calls your server, and your server calls the provider.
Should my app use certificate pinning?
It depends on the risk. Pinning protects against some interception attacks, but it can stop every installed copy connecting if certificates change unexpectedly. Apps handling payments or sensitive data often use it with backup keys and a rotation plan, while many business apps gain more from a well-secured API.
Should my app block jailbroken or rooted phones?
Blocking them outright can turn away legitimate users and is easy for attackers to get around. A better approach is to check attestation from Apple and Google on the server, and to limit high-risk actions when a device fails the check. Decide based on what the app protects.
What is the OWASP Mobile Top 10?
The OWASP Mobile Top 10 is a list of the most significant security risks for mobile apps, published by the Open Worldwide Application Security Project. It is updated periodically and is widely used as a starting point for design reviews and testing, alongside OWASP’s more detailed verification standard.