We use Keychain Access in the Radix Wallet, it is a very popular package, it has not been updated in a while because the Keychain APIs does not really change. So yes I consider it safe to use. You can also take a look at Valet, but I don't like that they want us to use their identifier, does not really mean anything, but feels more... "boxed in".
I think it would be better to leave that feature with the Secure Enclave
What do you mean? which feature? You cannot use Secure Enclave. It does not support symmetric encryption.
A common misconception is that not using Secure Enclave is not safe... that is not true, Apples "root Keychain key" itself is stored in Secure Enclave, so whatever you store in ""just"" Keychain, is safe!
It probably wouldn’t be possible to sync it with iCloud.
Correct, that is not possible - yet again, not relevant for you and your needs - but an Elliptic Curve Cryptography private key, e.g. on curve P256
store in Secure Enclave cannot leave the Secure Enclave, that is the whole point! The Secure Enclave is a HSM
: hardware security module, much like e.g. Ledger Hardware wallets, and Elliptic Curve Cryptography signing happens inside of the HSM. But again, this is irrelevant for you.
I’m facing a tough decision between security and availability. I believe I need to make a trade-off.
Go with availability is my recommendation.
kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly is typically for crypto wallets, where we protect a user entire life savings... I think the frustration of a user not being able to access her previous (health) data would have a bigger negative impact.
do I need to enable the iCloud capability for the app?
(reversed the order of your two last questions...)
No, this is NOT iCloud container. This is Keychain with Apples own extra feature of gratis also being able to sync it to iCloud. If and only if end user has enabled that config in iOS settings!!!.
When I initialize the key, do I need to check if it’s already stored in iCloud
No. I think you would benefit from thinking about Keychain (possibly with iCloud sync, once again, not iCloud container) as UserDefault
, but safe, and if end user has enable config of Keychain iCloud sync and if you set the key-value item as synchronizable(true)
it will be synced to iCloud.
Just generate a SymmetricKey, save it to Keychain. Done. Use it.