General Questions
When should I use SAMKeychain instead of raw Security.framework?
When should I use SAMKeychain instead of raw Security.framework?
Use SAMKeychain when:
- You need simple password/token storage
- You want readable, maintainable code
- You don’t need advanced keychain features (certificates, keys, etc.)
- You want cross-platform support (iOS/macOS/tvOS/watchOS)
- You need certificate or cryptographic key management
- You need advanced query capabilities
- You need features SAMKeychain doesn’t expose
Which accessibility type should I use?
Which accessibility type should I use?
Choose based on your app’s needs:⚠️ Avoid:
kSecAttrAccessibleWhenUnlocked (Recommended for most apps)- Most secure option
- Data accessible only when device is unlocked
- Use for: Passwords, auth tokens, sensitive user data
kSecAttrAccessibleAfterFirstUnlock (Background apps)- Data accessible after first unlock, even if device locks again
- Use for: Background sync tokens, notification tokens
kSecAttrAccessibleWhenUnlockedThisDeviceOnly (High security)- Most secure, never syncs to iCloud
- Use for: Extremely sensitive data, biometric-protected data
kSecAttrAccessibleAlways- Deprecated and insecureNULL(default) - Uses system default, which is insecure
Can I share keychain items between multiple apps?
Can I share keychain items between multiple apps?
Does SAMKeychain work with Swift?
Does SAMKeychain work with Swift?
Yes! SAMKeychain works seamlessly with Swift:With error handling:
The library includes
__attribute__((swift_error(none))) for proper Swift error handling integration.How do I handle background access to the keychain?
How do I handle background access to the keychain?
Problem: App needs to access keychain while running in background, but device might be locked.Solution: Use When to use:
kSecAttrAccessibleAfterFirstUnlock:- Background fetch
- Push notification handling
- Background audio/location apps
- VoIP apps
kSecAttrAccessibleWhenUnlocked, but still secure (data is encrypted at rest).Alternative for high security: Store critical data with WhenUnlocked, less critical data with AfterFirstUnlock:How secure is data stored in the keychain?
How secure is data stored in the keychain?
Very secure. The keychain is one of the most secure storage options available:Security features:
- ✅ AES-256 encryption at rest
- ✅ Hardware-backed on devices with Secure Enclave
- ✅ Isolated from app sandbox - survives app deletion
- ✅ Protected by device passcode/biometrics
- ✅ Encrypted in device backups
- Passwords and tokens
- Encryption keys
- Certificates
- Other sensitive data
- Data becomes accessible if device is jailbroken/compromised
- Data in iCloud Keychain is encrypted but stored on Apple servers
- On older devices without Secure Enclave, encryption is software-based
The keychain is appropriate for passwords, tokens, and small secrets. For large amounts of sensitive data, use encrypted files with keychain-stored encryption keys.
Technical Questions
Can I store binary data, not just strings?
Can I store binary data, not just strings?
Yes! Use Store any NSCoding-compliant object:
passwordData for binary data:How does iCloud Keychain synchronization work?
How does iCloud Keychain synchronization work?
Requirements:Synchronization modes:When to use:
- iOS 7+ or macOS 10.9+
- User must be signed in to iCloud
- iCloud Keychain must be enabled in Settings
- ✅ Login credentials (sync across user’s devices)
- ✅ API tokens (if same token works on all devices)
- ❌ Device-specific secrets
- ❌ Extremely sensitive data (use
ThisDeviceOnlyaccessibility)
- Sync doesn’t work in iOS Simulator
- Items saved with sync can’t be fetched without specifying sync mode
- Sync timing is controlled by iOS, not your app
Use
synchronizationMode = SAMKeychainQuerySynchronizationModeAny when fetching if you’re not sure whether the item was saved with sync enabled.What happens to keychain data when app is deleted?
What happens to keychain data when app is deleted?
Behavior varies by platform:iOS:Clear all keychain data on logout:
- Keychain items are usually deleted when app is uninstalled
- Items may persist if:
- App installed via TestFlight
- App installed via Enterprise distribution
- Items use iCloud Keychain sync
- Items in shared access groups persist if other apps in group remain
- Keychain items always persist after app deletion
- User must manually delete via Keychain Access.app
- This is by design for security/data recovery
- Similar to iOS behavior
Can I use SAMKeychain in an app extension?
Can I use SAMKeychain in an app extension?
Yes, but with considerations:1. Share keychain between app and extension using access groups:2. Add entitlement to both targets:3. Consider accessibility for background extensions:Extension types and keychain access:
App.entitlements & Extension.entitlements
| Extension Type | Keychain Access | Notes |
|---|---|---|
| Today Widget | ✅ Full access | Use access groups to share with app |
| Share Extension | ✅ Full access | Use access groups to share with app |
| Keyboard Extension | ⚠️ Restricted | Limited keychain access for security |
| Watch App | ✅ Full access | Has its own keychain, use iCloud sync or Handoff |
| Action Extension | ✅ Full access | Use access groups to share with app |
How do I migrate keychain data to a new device?
How do I migrate keychain data to a new device?
Keychain data is automatically migrated:1. Via iCloud Keychain (if enabled):2. Via encrypted iOS backup:Manual migration approach:If you need custom migration logic:
- Items saved with
synchronizationMode = SAMKeychainQuerySynchronizationModeYes - Automatically sync to new device when user signs in to iCloud
- All keychain items (except
ThisDeviceOnly) are included in encrypted backups - Restored when user restores from backup on new device
kSecAttrAccessibleWhenUnlockedThisDeviceOnlykSecAttrAccessibleWhenPasscodeSetThisDeviceOnly- Any accessibility type ending in
ThisDeviceOnly
Does SAMKeychain support Touch ID / Face ID authentication?
Does SAMKeychain support Touch ID / Face ID authentication?
SAMKeychain doesn’t directly expose biometric authentication, but you can layer it on top:Approach 1: Use Approach 3: Store a separate encryption key in keychain:
SecAccessControlCreateWithFlags (advanced):SAMKeychain doesn’t expose this, so you’d need to use Security.framework directly for items that require biometric authentication.Approach 2: Add biometric check before keychain access:For true hardware-backed biometric protection, you’ll need to use Security.framework’s
kSecAccessControlBiometryAny flag, which SAMKeychain doesn’t expose.Still have questions?
Troubleshooting
Debug common issues and errors
GitHub Issues
Search existing issues or ask a question
