5

encrypted-core-data用来加密所有持久化的数据,以前很简单CoreDatapersistentStoreCoordinator创建代码如下。

- (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 上显示了如此多的崩溃

crashlytics 崩溃日志图像

AESCrypt用于加密密码,然后使用KeychainItemWrapper.

观察:
crashlytics 显示的崩溃仅在我们使用分发配置文件在测试飞行上上传构建时才会出现。

据 crashlytics 报道,iOS 11 上发生 100% 崩溃

4

1 回答 1

0

我认为这是您看到的 iOS 10 中的一个已知错误,有一种解决方法:启用“钥匙串共享”(在您的应用程序 -> Xcode 中的功能选项卡下)。

iOS10 上的 KeychainItemWrapper 崩溃

于 2018-05-16T18:06:03.697 回答