我encrypted-core-data
用来加密所有持久化的数据,以前很简单CoreData
。persistentStoreCoordinator
创建代码如下。
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *oldStoreURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"VistaJetApp.sqlite"];
NSURL *newStoreURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"VistaJet.sqlite"];
NSError *error = nil;
NSString *currentPassword = [[VJAesCryptoWrapper getInstance] getCurrentPassword];
NSDictionary *options = [self getEncryptedStoreOptionsWithPassword:currentPassword andDatabaseStore:newStoreURL];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
//if old store not exists , it means fresh installation
if([[NSFileManager defaultManager] fileExistsAtPath:oldStoreURL.path] == NO) {
if (![_persistentStoreCoordinator addPersistentStoreWithType:EncryptedStoreType configuration:nil URL:newStoreURL options:options error: &error]) {
}
} else {
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:oldStoreURL options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error: &error]) {
}
NSPersistentStore *oldUnsecureStore = [_persistentStoreCoordinator persistentStoreForURL:oldStoreURL];
[ConsoleLogger logText:[NSString stringWithFormat:@"Migration started"]];
//start migration
if(![_persistentStoreCoordinator migratePersistentStore:oldUnsecureStore toURL:newStoreURL options:options withType:EncryptedStoreType error:&error]) {
} else {
[[NSFileManager defaultManager] removeItemAtURL:oldStoreURL error:nil];
}
}
return _persistentStoreCoordinator;
}
创建选项字典
- (NSDictionary*)getEncryptedStoreOptionsWithPassword:(NSString*)password andDatabaseStore:(NSURL*)storeUrl {
return @{ EncryptedStorePassphraseKey: password,
EncryptedStoreDatabaseLocation: storeUrl,
NSMigratePersistentStoresAutomaticallyOption:@YES,
NSInferMappingModelAutomaticallyOption:@YES
};
}
我正在使用钥匙串中保存密码,并且我的代码完全在方法KeychainItemWrapper
上崩溃。getEncryptedStoreOptionsWithPassword:currentPassword
应用程序已上线,我无法重现崩溃,但在 crashlytics 上显示了如此多的崩溃
还AESCrypt
用于加密密码,然后使用KeychainItemWrapper
.
观察:
crashlytics 显示的崩溃仅在我们使用分发配置文件在测试飞行上上传构建时才会出现。
据 crashlytics 报道,iOS 11 上发生 100% 崩溃