Ich bin mit Xcode 7.3 für iOS 9.3, um zu versuchen und eine Core Data-Datei zu verschlüsseln. Ich versuche, NSPersistentStoreFileProtectionKey zu verwenden und auf NSFileProtectionComplete festzulegen, um die Verschlüsselung zu aktivieren. Es ist aus irgendeinem Grund nicht funktioniert und ich kann immer die .sqlite Datei von der App und klicken Sie sich durch den Inhalt in sqlitebrowser oder iexplorer erzeugt sehen. Hier ist mein Code:NSFileProtectionComplete nicht verschlüsselt die Kerndatendatei
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
var failureReason = "There was an error creating or loading the application's saved data."
let dict: [NSObject : AnyObject] = [
NSPersistentStoreFileProtectionKey : NSFileProtectionComplete
]
do {
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: dict)
} catch {
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error as NSError
let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
abort()
}
do {
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
try NSFileManager.defaultManager().setAttributes([NSFileProtectionKey : NSFileProtectionComplete], ofItemAtPath: url.path!)
} catch {
}
do {
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite-wal")
try NSFileManager.defaultManager().setAttributes([NSFileProtectionKey : NSFileProtectionComplete], ofItemAtPath: url.path!)
// try print(NSFileManager.defaultManager().attributesOfFileSystemForPath(String(url)))
} catch {
}
do {
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite-shm")
try NSFileManager.defaultManager().setAttributes([NSFileProtectionKey : NSFileProtectionComplete], ofItemAtPath: url.path!)
// try print(NSFileManager.defaultManager().attributesOfFileSystemForPath(String(url)))
} catch {
}
return coordinator
}()
Ich habe auch Data Protection für mein Ziel in den "Fähigkeiten" aktiviert. Ich habe das Provisioning-Profil vom Apple Developer Portal neu erstellt und verwende dieses mit Enabled Data Protection.
Ich bin auch mit dem folgenden Code die Dateiattribute von .sqlite, .sqlite-wal und .sqlite-shm-Dateien zu überprüfen. NSFileProtectionKey ist für alle 3 korrekt eingestellt.
func checkProtectionForLocalDb(atDir : String){
let fileManager = NSFileManager.defaultManager()
let enumerator: NSDirectoryEnumerator = fileManager.enumeratorAtPath(atDir)!
for path in enumerator {
let attr : NSDictionary = enumerator.fileAttributes!
print(attr)
}
}
Ich habe auch versucht die Journal-Modus zu deaktivieren -wal und -shm Dateien zu verhindern, erstellt werden. Aber ich kann immer noch die .sqlite Datei lesen. Obwohl die Attribute NSFileProtectionComplete lesen.
Wie bei Apple Docs in der Apple-Dokumentation unter „On Disk Encryption mit Daten zu schützen“, versuchte ich, ob der Wert der Variablen protectedDataAvailable Änderungen zu überprüfen, wie unter
public func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
NSThread.sleepForTimeInterval(10)
sleep(10)
let dataAvailable : Bool = UIApplication.sharedApplication().protectedDataAvailable
print("Protected Data Available : " + String(dataAvailable))
}
im Code angezeigt Wenn ich das überprüfen Wert ohne die Verzögerung ist es auf True gesetzt, aber nach dem Hinzufügen der Verzögerung ist es auf false gesetzt. Dies ist eine Art ermutigend, aber gleich nach, wenn ich die Container herunterzuladen, um den Inhalt zu zeigen, ist es noch .sqlite Datei hat, das immer noch den Inhalt zeigt, wenn in sqlitebrowser geöffnet.
Wie testen Sie? Wie ist der Status der App (offen/geschlossen/erledigt) und des Geräts (offen/gesperrt) beim Testen? – Wain
Ich habe die App im Öffnen/Schließen und Gerät gesperrt/entsperrt Zustände getestet. Ein Szenario, bei dem ich hoffte, dass es immer funktionieren würde, war, als das Gerät gesperrt war, aber mit Xcode verbunden war und ich den Container mit einem neuen Zeitstempel und einer neuen Versionsnummer heruntergeladen hatte, aber die Daten trotzdem lesen konnte. – EmbCoder