touchlab / SQLiter

Minimal multiplatform sqlite library
https://touchlab.co
173 stars 35 forks source link

Exclude DB from backup/icloud (iOS) #103

Open dusiema opened 1 year ago

dusiema commented 1 year ago

I use sqldelight in a KMM app project. I store sensitive cached data, mostly in the form of base64 encoded images as strings. It's only meant to be used as a cache when there is no network.

I want to avoid the underlying SQLite db from being included in an iOS backup or iCloud sync/backup.

Theres is a key that one can set for this: NSURLIsExcludedFromBackupKey.

How can I set this on the SQLite DB for the native iOS part of the app?

dusiema commented 1 year ago

I ended up setting the attribute right after the NativeSqliteDriver is created by using this function:

    @OptIn(ExperimentalForeignApi::class)
    fun setExcludeFromBackup() {
        return memScoped {
            val databasePath = DatabaseFileContext.databasePath(databaseName, null)
            if (NSFileManager.defaultManager.fileExistsAtPath(databasePath)) {
                val errorRef = alloc<ObjCObjectVar<NSError?>>()
                Napier.i { "Excluding ${databasePath} from iCloud/Backup!" }
                val success = NSURL.fileURLWithPath(databasePath).setResourceValue(NSNumber(bool = true), NSURLIsExcludedFromBackupKey, errorRef.ptr)
                if (!success) {
                    Napier.i { "Excluding ${databasePath} from iCloud/Backup failed! Reason: ${errorRef.value}" }
                }
            }
        }
    }

I have yet to test if this does work at all by really doing a backup of a real iphone and check if the database file is not included. So be aware this is untested.

dusiema commented 1 year ago

Side note: I pulled a backup from an iPhone but seemingly developer signed apps are not included in an iPhone backup anyways.