iOS security with AI agents: Keychain, CryptoKit, App Attest and audits
Do not let the agent put tokens in UserDefaults. Point it at Keychain Services with a SecAccessControl policy, CryptoKit for encryption, and App Attest when you need device integrity. Install a security skill (or Axiom's security suite), then require an OWASP-style audit before merging credential or crypto changes.
Last updated:
This page is about getting a coding agent to store secrets correctly, encrypt on-device data, and audit iOS apps for common security mistakes. For StoreKit receipt checks, see StoreKit and in-app purchases with AI agents. For the Codex OS sandbox, see Codex iOS build troubleshooting (live guide). Location authorization is separate from Keychain and privacy manifests; see iOS Core Location permissions with AI agents.
What the agent has to get right
Apple's Security framework is the contract for credentials and trust on Apple platforms. Agents trained on blog posts still invent UserDefaults token storage, ignore OSStatus, and invent AES APIs that are not CryptoKit.
Four layers show up in almost every security PR:
Credential storage — Keychain Services with an appropriate
kSecAttrAccessiblelevel and, when needed,SecAccessControl+ biometrics via Local Authentication.Cryptography — Apple CryptoKit for hashing, HMAC, AES-GCM / ChaChaPoly and key agreement; avoid hand-rolled crypto.
App integrity — Establishing your app's integrity covers App Attest (
DCAppAttestService) when you need to prove the binary to your server.Declarations and review — Privacy manifest files for required-reason APIs (iOS privacy manifests with AI agents); pair with an OWASP MASVS-style audit before merge.
Rules worth adding to AGENTS.md or CLAUDE.md:
Security: never store tokens or passwords in UserDefaults or plain files. Use Keychain Services with an explicit accessibility and access-control policy. Prefer CryptoKit over third-party or DIY crypto. Use App Attest when the server must trust the client binary. Run a security skill audit on credential and crypto diffs. Confirm APIs on developer.apple.com.
Step 1: install a security skill
Ask the agent to:
Install Swift Security Expert (
npx skills add https://github.com/ivan-magda/swift-security-skill --skill swift-security-expert) for Keychain, biometrics, CryptoKit and OWASP-mapped guidance.Or install Axiom (
/plugin marketplace add CharlesWiltgen/Axiom, then install the axiom plugin) and invoke its security skill for Keychain, CryptoKit, passkeys, App Attest and code signing.add Mobile Security Skill for a multi-agent OWASP MASVS/MASTG audit loop.
Honest limit: one dedicated security skill plus a short AGENTS.md rule beats installing every audit pack at once.
Step 2: Keychain before UserDefaults
Point the agent at Keychain Services. Require:
Generic passwords or keys via
SecItemAdd/SecItemCopyMatching, notUserDefaultsfor secrets.Accessibility chosen on purpose (
kSecAttrAccessibleWhenUnlockedThisDeviceOnlyis a common default for session tokens).Access control with
SecAccessControlCreateWithFlagswhen biometrics should gate read.Error handling for
OSStatus(includingerrSecDuplicateItemanderrSecItemNotFound) instead of ignoring return codes.
Step 3: CryptoKit and App Attest
For encryption and integrity:
Prefer CryptoKit types (
AES.GCM,ChaChaPoly,HMAC,P256/Curve25519) over inventing nonce or padding logic.When the backend must trust the client, follow Establishing your app's integrity and
DCAppAttestService—do not invent a custom "device fingerprint".Keep privacy manifests updated when the change touches required-reason APIs.
Step 4: audit before merge
Run the security skill (or Axiom's security-privacy scanner) on the credential/crypto diff. Treat CRITICAL/HIGH findings—UserDefaults secrets, missing access control, DIY crypto—as merge blockers. An audit skill does not replace a human review of threat model and server-side checks.
Tools compared (4)
| Tool | Best for | Agents (README) | Install (README) | License | Stars | Last push |
|---|---|---|---|---|---|---|
| Axiom (CharlesWiltgen) | Full xOS skill suite with a dedicated security pack: Keychain, CryptoKit, passkeys, App Attest, file protection, code signing | Claude Code, Codex | /plugin marketplace add CharlesWiltgen/Axiom then install axiom from /plugin | MIT | 1242 | 31 Oct 2026 |
| Swift Security Expert (ivan-magda) | Focused Keychain, biometric, CryptoKit, Secure Enclave and OWASP-mapped guidance with correct/incorrect pairs | Claude Code, Cursor, Codex | npx skills add https://github.com/ivan-magda/swift-security-skill --skill swift-security-expert | MIT | 40 | 17 Jun 2026 |
| iOS Agent Skills (rusel95) | Benchmarked pack that includes a dedicated ios-security skill alongside architecture and testing skills | Claude Code, Codex | npx skills add rusel95/ios-agent-skills --skill swiftui-mvvm (swap --skill for ios-security) | MIT | 13 | 13 Oct 2026 |
| Mobile Security Skill (harshdhamaniya) | Claude Code skill with 18 subagents for OWASP MASVS/MASTG-aligned iOS and Android audits (plist, storage, crypto, deep links) | Claude Code, Codex | git clone https://github.com/harshdhamaniya/mobile-security-skill.git then copy skills/mobile-security-skill into ~/.claude/skills/ | MIT | 21 | 8 Oct 2026 |
⚠ = no push for more than 4 weeks. Axiom (31 Oct) is fresh. ios-agent-skills-rusel95 (13 Oct) and mobile-security-skill (8 Oct) are within a month. Swift Security Expert (17 Jun) is older—verify guidance against current Keychain and CryptoKit docs before treating every recommendation as final.
Which one to pick:
Default dedicated security skill: Swift Security Expert.
Already on Axiom for Apple platforms: use Axiom's security skill instead of a second pack.
Benchmark-backed multi-topic pack that includes security: iOS Agent Skills (rusel95).
deep OWASP MASVS audit: Mobile Security Skill.
A prompt that keeps the agent honest
Setting up the agent itself: Claude Code for iOS development, Codex for iOS development or Cursor rules for SwiftUI.
Store the refresh token in the Keychain with WhenUnlockedThisDeviceOnly and biometric access control. Do not use UserDefaults. Encrypt the local cache with CryptoKit AES.GCM. Add App Attest only if the server must verify the binary. Run a security skill audit on the diff and list severity plus fix for each CRITICAL/HIGH finding. Confirm Keychain and CryptoKit APIs on developer.apple.com.
FAQ
- What is iOS security work for AI coding agents?
- iOS security work for agents means teaching Claude Code, Cursor or Codex to store credentials in Keychain Services, encrypt with CryptoKit, use App Attest when the server must trust the client, declare privacy manifests, and run an OWASP-style audit instead of inventing UserDefaults token storage or DIY crypto from training data.
- Should the agent store API tokens in UserDefaults?
- No. UserDefaults is not a secret store. Apple's Keychain Services API is the supported place for passwords, tokens and keys. Require an accessibility level and, when the product needs it, SecAccessControl with Local Authentication before the agent reads the item.
- What is App Attest, and when should the agent add it?
- App Attest (DCAppAttestService), documented under Establishing your app's integrity, lets your server verify that a request comes from your genuine app binary on a genuine device. Use it when fraud or modified clients matter; do not invent a custom device fingerprint as a substitute.
- Which security tool should I install first?
- Start with Swift Security Expert for focused Keychain and CryptoKit guidance, or Axiom if you already use that suite and want its security pack. Add rusel95's ios-security skill when you want a benchmarked multi-topic pack. Mobile Security Skill helps with MASVS/MASTG audit loops.
- Does a passing security-skill audit mean the app is secure?
- No. Skills catch common client-side mistakes (UserDefaults secrets, weak Keychain settings, DIY crypto). They do not replace threat modeling, server-side verification, or a human review of entitlements and privacy manifests.