创建选项时应用程序崩溃addPersistentStoreWithType方法

时间:2018-05-16 17:58:05

标签: app-store core-data-migration nspersistentstore keychainitemwrapper encrypted-core-data-sql

我正在使用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 crash logs image

还使用AESCrypt加密密码,然后使用KeychainItemWrapper将其保存到钥匙串。

观察:
只有当我们使用分发配置文件在测试航班上上传构建时,才会显示崩溃关系所显示的崩溃。

据崩溃报告

报道,iOS 11上的崩溃率达到100%

1 个答案:

答案 0 :(得分:0)

我认为这是iOS 10中的一个已知错误,您可以看到,有一种解决方法:启用“Keychain Sharing”(在您的应用程序下 - &X; Xcode中的功能选项卡)。

KeychainItemWrapper crash on iOS10