
Use Keychain for sensitive credentials, Core Data for structured app data, and Realm for secure, cross-platform storage. Always enable encryption, store keys in Keychain, and avoid saving secrets in plain text. We recommend combining these approaches, each fills a security gap the others leave open.
Key Takeaway
- Keychain is best for credentials and encryption keys, never for large blobs or files.
- Core Data and Realm require extra encryption steps for sensitive user data.
- Store encryption keys in Keychain, and never hard-code secrets in your app.
Core Data for Structured Data Management
We see Core Data used everywhere in iOS development bootcamps, especially for apps with lots of structured information. Core Data is not just a database, it’s an object graph manager and persistence framework. We like describing it as a way to make your iOS app’s data behave more like a living, changing network of objects instead of a boring spreadsheet.
Core Data Architecture and Use Cases
In practice, Core Data sits between your app and your data, using SQLite as the default backend (though you can swap it out if needed). The framework handles objects and their relationships, so we can focus on logic rather than writing SQL queries.
It’s ideal for things like user profiles, app settings, and any situation where data is linked together and queried a lot. This fits well with best practices in secure mobile coding, where maintaining data integrity during ios development is crucial.
Typical use cases we’ve seen in training include:
- Managing user-generated content (think notes, bookmarks, or activity logs)
- Caching network responses for offline access
- Handling complex configuration data (plenty of relationships and attributes)
Object Graph Management and Persistence Framework
Core Data keeps an in-memory representation of your objects, so you can update a user’s profile photo, add a new note, or edit settings without worrying about how it all gets saved. When we call save(), it persists to disk. This is efficient but can lead to confusion about when data is actually written, so we always remind trainees to learn about the Core Data stack, especially managed object contexts.
Typical Data Modeling and Querying
We like to emphasize using Xcode’s data modeling tools. You define entities, attributes, and relationships visually, which is a lifesaver for beginners. Queries are performed using NSFetchRequest, and you can filter or sort data with predicates, handy for building search features.
Security Mechanisms in Core Data
Now for the part our bootcamp students always ask about, how secure is the data?
File System-level Encryption and Data Protection Classes
Credits: IBM Technology
On iPhones and iPads with a passcode, every file, including Core Data’s SQLite store, is encrypted at the file system level. Apple uses Data Protection classes to decide when files can be accessed. By default, Core Data uses NSFileProtectionCompleteUntilFirstUserAuthentication, which means files are inaccessible until the user unlocks the device after a reboot. These protections align with Apple’s ios app transport security requirements to ensure sensitive data is protected in transit and at rest, reinforcing overall app security.
- Data Protection classes: Choose the strictest class your app can tolerate. For example, NSFileProtectionComplete is safest but might block access if the device is locked.
- Practical tip: Set the protection class when configuring your persistent store. [1]
App Sandbox and Access Control
Each app’s data is sandboxed. Another app (unless it’s in the same app group or the device is jailbroken) can’t touch your files. This is enforced by the operating system.
Options for Custom Encryption Enhancements
File system encryption is not foolproof. We often add custom encryption, encrypting sensitive fields before saving or using an encrypted SQLite library. For example, before storing a credit card number, encrypt it in-memory, then save the encrypted blob. This way, even if the backup is exposed, the data is unreadable without your key.
Limitations and Security Recommendations
Unsuitability for Highly Sensitive Credentials
We never recommend using Core Data for passwords, tokens, or encryption keys. Keychain exists for a reason, it’s hardware-backed and isolated from the rest of the app.
Backup Exposure Risks and Mitigation
By default, Core Data stores are included in iTunes and iCloud backups. If your app handles sensitive data, you should:
- Exclude certain files from backup with the NSURLIsExcludedFromBackupKey
- Or, encrypt data before storage
Integrating Additional Encryption Layers
For high-security apps, we advise encrypting data before placing it in Core Data, then saving the encryption key in the Keychain. That way, even if the SQLite file leaks, the data is safe.
Realm Database for Cross-platform Data Storage
We find Realm especially appealing for teams building both iOS and Android apps, due to its cross-platform nature. It’s easy to set up, and the API feels modern. Realm is not built into iOS like Core Data is, but it’s open source and has an active community.
Developers should keep swift secure coding guidelines in mind when managing encryption keys and sensitive data within Realm to prevent leaks and follow ios secure coding principles.
Introduction to Realm and Its Features
Realm offers live objects, zero-copy architecture (meaning less memory use), and queries that feel like Swift collections. Developers don’t worry about schema migrations as much, thanks to Realm’s tooling.
Alternative to Core Data and SQLite
Most of our bootcampers try Realm when Core Data’s complexity feels overwhelming. Realm provides a straightforward API, no need for managed object contexts, and automatic change notifications.
Ease of Use and Cross-platform Capabilities
Realm supports iOS, Android, and even Apple TV. Schema changes are managed with migration blocks. This flexibility is helpful for teams working across platforms or planning to sync data.
Security Features of Realm
Built-in AES-256 Encryption and Implementation Details
Realm can encrypt all data stored on disk with AES-256. You enable encryption by passing a 64-byte key when configuring the Realm database. If you forget to do this, there’s no encryption, so we always triple-check our configs. [2]
- Implementation: On iOS, Realm uses Apple’s CommonCrypto, so cryptography meets industry standards.
- Encryption Scope: The entire database file is encrypted, not just individual fields.
Encryption Key Management Practices
Never hard-code the key in your source code. We always store encryption keys in the Keychain and retrieve them at runtime.
- Practical Example: Generate a random 64-byte key using SecRandomCopyBytes, store it in Keychain, then provide it during Realm configuration.
Encryption Scope and Cryptographic Standards
Realm’s encryption is all-or-nothing. Either the database is encrypted, or it’s not. This avoids leaks but means you must manage your encryption key carefully.
Best Practices with Realm
- Enable encryption for every database containing sensitive user data.
- Store encryption keys in Keychain, not in plain text or property lists.
- When supporting data sharing between devices (iCloud, multiple devices), manage keys for each device separately.
- Never include backup files with unencrypted data.
Comparative Analysis and Implementation Strategies
At our bootcamp, we spend a lot of time helping students choose the right tool for the job. Each storage solution has strengths and weaknesses. Here’s what we’ve learned (sometimes the hard way).
Feature Comparison of Keychain, Core Data, and Realm
Feature | Keychain | Core Data | Realm |
Purpose | Credentials, tokens, keys | Structured app data | Structured app data |
Encryption | AES-256-GCM, hardware-backed | File system, custom possible | AES-256 (optional) |
Data Size | Small | Large | Large |
Access Control | App/access group | App sandbox | App sandbox |
Backup | Device-bound, encrypted | Included in backup | Included in backup |
Custom Encryption | Not needed | Possible (manual) | Built-in (must enable) |
Key Management | Automatic | Manual (if custom) | Manual (Keychain) |
Purpose and Suitable Data Types
- Keychain: Use for small, sensitive items (passwords, tokens, private keys)
- Core Data and Realm: Use for structured app data (profiles, settings, offline content)
Encryption Types and Security Levels
- Keychain: Hardware-backed, best for secrets
- Core Data: File system encryption, can layer custom encryption
- Realm: AES-256 if enabled, key management is your responsibility
Data Size Handling and Access Control
- Keychain is not for blobs or files.
- Core Data and Realm handle large datasets.
- Both Core Data and Realm rely on the app sandbox to enforce access, but sandboxing is no substitute for encryption.
Backup Inclusion and Custom Encryption Support
- Keychain secrets are device-bound and protected even in backups.
- Core Data and Realm files are backed up unless you exclude them. Encrypt files or fields before saving if you manage sensitive user data.
Integrating Technologies for Enhanced Security
We often combine these technologies for better results. Keychain is used to store encryption keys, while Core Data or Realm hold encrypted data.
Combining Keychain with Core Data or Realm
A pattern we use:
- Generate a random encryption key.
- Store the key in Keychain.
- Use the key to encrypt user data in Core Data or Realm.
This separates the secrets from the storage, reducing risk if a file leaks.
Storing Encryption Keys Versus Data
Never store encryption keys alongside encrypted data. We always use Keychain for keys, Core Data or Realm for encrypted blobs.
Balancing Usability and Security in App Design
Too much security can frustrate users, especially if they lose access after a forgotten passcode. We work with product teams to set realistic defaults, like using Data Protection classes that allow background fetch, or offering secure but memorable password reset flows.
Practical Recommendations for Developers

After years of training new iOS developers, these are our best tips:
Selecting Storage Solutions Based on Data Sensitivity
- Use Keychain for anything you wouldn’t want in a database, credentials, tokens, private keys.
- Use Core Data or Realm for user-generated content, settings, and other structured data.
Ensuring Secure Backup and Restore Processes
- Exclude sensitive files from iCloud and iTunes backups unless you’ve encrypted them yourself.
- For extra protection, encrypt sensitive database fields and store the key in Keychain.
Leveraging Hardware-backed Security Features
- Take advantage of Secure Enclave for key generation and storage if available.
- Always use Data Protection classes appropriate for your app’s needs.
- Avoid storing secrets in plain text or property list files.
FAQ
How does iOS Keychain help with storing sensitive user data securely compared to local storage options like plist files or Core Data?
iOS Keychain is designed for secure storage of sensitive data, such as passwords, credit card info, or class keys. Unlike plist files or Core Data, which store local data often in plain text or SQLite databases, Keychain uses hardware security features like the Secure Enclave processor.
This helps prevent unauthorized access by enforcing access control and requires a specific privilege level during the boot process, making it safer for ios applications.
Can Core Data be trusted for real-time data persistence when handling sensitive user data on Apple devices?
Core Data is useful for data persistence in ios applications, especially for structured data stored in a sqlite database. However, it does not natively encrypt data or offer hardware security like the secure enclave.
Because of this, sensitive user data stored locally with Core Data should be encrypted by the developer or combined with other security features. It’s best for scenarios where real-time access and data structure matter but not for storing sensitive data alone.
What role does Realm play in secure data storage on Apple silicon and ios devices compared to other local storage options?
Realm database is an open source alternative to Core Data and sqlite databases, popular for mobile app development due to ease of use and real-time syncing capabilities.
While Realm configuration allows fast local data storage, it lacks built-in hardware security like ios keychain services or secure enclave protections. Developers should encrypt sensitive data before storing it in Realm to meet app security requirements on Apple platform security.
Why might an ios application developer encounter “return nil” when accessing keychain items, and how does access groups impact this?
“Return nil” occurs when the app tries to access keychain data without the correct access groups or required privilege level. Keychain access is managed by ios platform security and access control settings, which isolate keychain items by app or group.
If the keychain item isn’t shared properly between apps or extensions, or if code signing doesn’t match, the keychain data won’t be accessible, leading to a nil result in swift data queries.
How does the data protection API work with keychain data protection to secure user data on apple tv, mac computers, and ios devices?
The data protection API integrates with ios keychain and secure enclave to enhance app security on apple devices, including apple tv and mac computers with apple silicon. It enforces data encryption based on passcodes and passwords, controlling when keychain data is accessible (for example, only after device unlock).
This hierarchy level of security helps prevent direct memory access attacks and ensures sensitive data is stored only under the required privilege level during the operating system’s boot process.
Conclusion
Practical security means knowing when to use the right tool. We mix Keychain for credentials, Core Data for user settings, and Realm when speed matters, each with encryption turned on. Don’t assume defaults are safe. One device theft or rogue backup is all it takes. Always store keys securely and think like an attacker before one finds you.
Want to learn secure coding hands-on? Join the Secure Coding Practices Bootcamp and start shipping safer code.
References
- https://support.apple.com/guide/security/data-protection-classes-secb010e978a/web
- https://www.progress.com/blogs/use-aes-256-encryption-secure-data