SWIFT - Realm db encryption not working

时光怂恿深爱的人放手 提交于 2019-12-01 01:19:23
TiM

Realm's encryption feature applies only to the ability to encrypt whole .realm files. There's no feature to encrypt discrete objects within the .realm file and leave the rest as-is.

If you do want to go about doing this, I'm afraid you would need to roll the encryption system yourself.

If I was going to do this, I'd do it this way:

  1. Create a Realm Object subclass with an NSData property called encryptedData.
  2. Serialize any objects you wanted to encrypt to NSData using the NSCoding protocol. (Saving custom SWIFT class with NSCoding to UserDefaults)
  3. Encrypt that resulting NSData object using an encryption method of your choice (AES Encryption for an NSString on the iPhone)
  4. Take the resulting encrypted NSData object and save it to the encryptedData property in your Realm object.
  5. Reverse the process when you want to retrieve that data.

While this process would work, as you can see, it's a non-trivial amount of extra work, and you would also lose all of the speed and memory-saving benefits of Realm in the process.

I would encourage you to rethink your app's design, and see if it is feasible to use Realm's own encryption feature after all. Good luck!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!