Member-only story
Build a Secure SwiftUI Property Wrapper for the Keychain
Securely save data for any Codable type in the encrypted database of your iOS and macOS apps

When you want a property to be saved and loaded from the persistent storage called UserDefaults
, you can declare it like this:
@AppStorage(“MyKey”) var savedValue
When the app launches, the key is used to retrieve the value. If there is a value, savedValue
will have that value. When you change savedValue
, UserDefaults
is updated automatically. If you’re displaying the value anywhere in your SwiftUI (e.g. in a Text
), the value is updated there too. In other words, it acts a lot like an @State
property, allowing you to change the wrapped value and refresh SwiftUI without being warned that self
is immutable.
But UserDefaults
is inherently insecure.
To quote Axel Kee’s website fluffy.es:
“Previously, we have explained that UserDefaults saves data into plist. Using apps such as iExplorer, users can access the Library/Preferences folder of their iPhone and read / modify the UserDefaults plist data easily (eg: Change the boolean value of “boughtProVersion” from false to true, or change the amount of coins). Don’t ever store a boolean for checking if user has bought in-app…