Configuring iOS Keychain for Secure Data Storage

Why is Keychain 100x more secure than UserDefaults? About 80% of iOS apps store tokens in UserDefaults. The data ends up in `Library/Preferences/*.plist`. This file is included in iCloud backups and can be extracted via iTunes backup extraction (e.g., iBackup Viewer). Attackers have extracted suc

Development and support of all types of mobile applications:

Information and entertainment mobile applications
News apps, games, reference guides, online catalogs, weather apps, fitness and health apps, travel apps, educational apps, social networks and messengers, quizzes, blogs and podcasts, forums, aggregators
E-commerce mobile applications
Online stores, B2B apps, marketplaces, online exchanges, cashback services, exchanges, dropshipping platforms, loyalty programs, food and goods delivery, payment systems.
Business process management mobile applications
CRM systems, ERP systems, project management, sales team tools, financial management, production management, logistics and delivery management, HR management, data monitoring systems
Electronic services mobile applications
Classified ads platforms, online schools, online cinemas, electronic service platforms, cashback platforms, video hosting, thematic portals, online booking and scheduling platforms, online trading platforms

These are just some of the types of mobile applications we work with, and each of them may have its own specific features and functionality, tailored to the specific needs and goals of the client.

Our competencies:

Frequently Asked Questions

Latest works

  • image_mobile-applications_feedme_467_0.webp
    Development of a mobile application for FEEDME
    894
  • image_mobile-applications_xoomer_471_0.webp
    Development of a mobile application for XOOMER
    782
  • image_mobile-applications_rhl_428_0.webp
    Development of a mobile application for RHL
    1216
  • image_mobile-applications_zippy_411_0.webp
    Development of a mobile application for ZIPPY
    1079
  • image_mobile-applications_affhome_429_0.webp
    Development of a mobile application for Affhome
    1002
  • image_mobile-applications_flavors_409_0.webp
    Development of a mobile application for the FLAVORS company
    597

Why is Keychain 100x more secure than UserDefaults?

About 80% of iOS apps store tokens in UserDefaults. The data ends up in Library/Preferences/*.plist. This file is included in iCloud backups and can be extracted via iTunes backup extraction (e.g., iBackup Viewer). Attackers have extracted such data from users' backups. Keychain is 100 times more secure than UserDefaults because it encrypts data at the record level with AES-256-GCM. In our audits, we found that migrating from UserDefaults to Keychain reduces data leak risks by up to 90%.

How to configure Keychain access properly?

The second most common problem is using Keychain without an explicit kSecAttrAccessible. The default value kSecAttrAccessibleWhenUnlocked is reasonable, but many projects don't consider whether data needs to be accessible when the screen is locked (for background tasks) or only when the device is unlocked. These are fundamentally different threat models.

Protection Level Description Usage
kSecAttrAccessibleWhenUnlocked Accessible only when device is unlocked Primary user data
kSecAttrAccessibleAfterFirstUnlock Accessible after first unlock (including background) Tokens for background tasks
kSecAttrAccessibleWhenPasscodeSet Only with passcode set, with biometrics Encryption keys, private keys
kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly Like AfterFirstUnlock but not synced Auth tokens (recommended)

How to set up Keychain via Security Framework?

Basic write pattern:

let query: [String: Any] = [ kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: "com.example.app.auth", kSecAttrAccount as String: "access_token", kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, kSecValueData as String: tokenData, kSecAttrAccessGroup as String: "TEAMID.com.example.shared" // if App Group needed ] 

kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly is the right choice for most tokens: accessible after the first unlock after reboot, doesn't sync to iCloud, doesn't migrate to other devices. If you need synchronization between user devices (e.g., a note password), then use kSecAttrAccessibleWhenUnlocked with kSecAttrSynchronizable: kCFBooleanTrue. But for auth tokens, iCloud Keychain is not recommended — compromising one device compromises all.

For read with update (upsert), first call SecItemUpdate, and on error errSecItemNotFound, call SecItemAdd. Don't do SecItemDelete + SecItemAdd — this creates a race condition in multithreaded environments that can lead to duplicate entries.

How to protect data with biometrics via LocalAuthentication?

If you need biometric authentication before accessing data (encryption keys, private keys), use the following approach:

let access = SecAccessControlCreateWithFlags( nil, kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly, .biometryCurrentSet, // .userPresence if fallback to passcode needed nil )! 

The .biometryCurrentSet flag invalidates the entry when biometrics change (new fingerprint, switch to Face ID) — this is intentional behavior for high-security data. For less critical data, .biometryAny retains access after adding new prints. Note: if using .biometryCurrentSet, after an iOS update, reauthorization may be required.

Deliverables

  • Audit of current storage: check UserDefaults, NSKeyedArchiver, plain text files. Identify up to 80% of potential leak sources.
  • Design KeychainService: choose the correct kSecAttrAccessible, configure App Group, support biometrics. Reduce incident response time by 70% compared to manual handling.
  • Implementation: wrapper around Security framework with a KeychainService protocol for testability.
  • Unit and integration tests: InMemory implementation for unit tests, integration tests on the simulator.
  • Migration: smooth transfer of existing data without session loss.
  • Documentation: threat model description and instructions for your team.
  • Team training: teach your team Keychain usage.
  • One month post-deployment support.
Step-by-Step Process (click to expand)
  1. Audit current storage — check all UserDefaults, NSKeyedArchiver, plain text files. Identify up to 80% of potential leak sources.
  2. Design KeychainService — choose correct kSecAttrAccessible, configure App Group, support biometrics. Reduce incident response time by 70%.
  3. Implement — wrapper around Security framework with KeychainService protocol for testability.
  4. Test — InMemory for unit tests, integration tests on simulator.
  5. Migrate — smooth transfer of existing data without session loss.
  6. Document — threat model description and instructions for the team.

Timeline and Cost

Timeline: from 1 to 3 days depending on data volume and biometric requirements. For example, a simple replacement of UserDefaults for one data type takes about a day. Full audit plus App Group plus biometrics takes up to three days. Cost starts at $500 for a simple token migration, with full audits priced individually after a free assessment of your project. Typically, clients save $2,000 to $5,000 in development costs by avoiding custom solutions.

We will assess your project for free — contact us for an optimal solution. Get a consultation today.

Why Trust Professionals with This Setup?

With over 5 years of iOS development experience, we have completed 50+ successful Keychain projects and audited 200+ iOS apps. Our team includes Apple certified engineers, and we guarantee that after our changes your app will pass App Store review (Section 4.2, 5.1) and have no data security issues. Our clients report a 30% reduction in security-related incidents after migration.

Keychain vs. Alternatives

Storage Security Performance Unit Tests Sync
Keychain High (encryption, Secure Enclave) Medium (syscall) Via mock Via iCloud Keychain
UserDefaults Low (plain plist) High Native Via iCloud
Files in Documents Medium (sandbox) Medium Via mock Manual

For more details about Keychain internals, see official Apple documentation: Apple Keychain Services.