0Pricing
Swift Academy · Lesson

Data Protection and Encryption

Protect files and use CryptoKit basics.

Data Protection on iOS

iOS encrypts files on disk by default, but you can choose when a file's contents are decryptable using Data Protection classes. These tie file access to the device lock state, much like Keychain accessibility classes do for secrets.

import Foundation
// File-level protection ties decryption to lock state.
// Set a protection class when writing sensitive files.

The Protection Classes

The main file protection levels are .complete (unreadable while locked), .completeUnlessOpen (stays readable if already open when locked), .completeUntilFirstUserAuthentication (readable after first unlock), and .none.

import Foundation
// FileProtectionType:
//   .complete
//   .completeUnlessOpen
//   .completeUntilFirstUserAuthentication (default)
//   .none
let level = FileProtectionType.complete
_ = level

All lessons in this course

  1. Storing Secrets in the Keychain
  2. Keychain Access Control
  3. Biometric Authentication
  4. Data Protection and Encryption
← Back to Swift Academy