@@ -146,13 +146,36 @@ - (FMDatabase *)openStoreDatabaseWithName:(NSString *)storeName key:(NSString *)
146146 return [[self class ] openDatabaseWithPath: fullDbFilePath key: key salt: salt error: error];
147147}
148148
149+ // If you created your database with an app based on Mobile SDK 12.0 or 12.1.x using cocoapod
150+ // Then SQLCipher was not properly linked
151+ // This method checks for that situation and encrypt the database if needed
152+ - (void )fixFor12Bug : (NSString *)storeName key : (NSString *)key salt : (NSString *)salt {
153+ NSString *fullDbFilePath = [self fullDbFilePathForStoreName: storeName];
154+
155+ __block BOOL needEncrypting = NO ;
156+ [[FMDatabaseQueue databaseQueueWithPath: fullDbFilePath] inDatabase: ^(FMDatabase* db) {
157+ // In the normal case, the db will not be readable - we don't want to be logging any errors
158+ BOOL logsErrors = db.logsErrors ;
159+ db.logsErrors = NO ;
160+ needEncrypting = [[self class ] verifyDatabaseAccess: db error: nil ];
161+ db.logsErrors = logsErrors;
162+ }];
163+
164+ if (needEncrypting) {
165+ [[self class ] encryptDbWithStoreName: storeName storePath: fullDbFilePath key: key salt: salt error: nil ];
166+ }
167+ }
168+
149169- (FMDatabaseQueue *)openStoreQueueWithName : (NSString *)storeName key : (NSString *)key salt : (NSString *)salt error : (NSError * __autoreleasing *)error {
150170
171+ [self fixFor12Bug: storeName key: key salt: salt];
172+
151173 __block BOOL result = YES ;
152174 NSString *fullDbFilePath = [self fullDbFilePathForStoreName: storeName];
153175 FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath: fullDbFilePath];
154176 [queue inDatabase: ^(FMDatabase* db) {
155177 result = ([[self class ] setKeyForDb: db key: key salt: salt error: error] != nil );
178+ result = result && [db goodConnection ]; // make sure SQLCipher is properly linked
156179 }];
157180 return (result ? queue : nil );
158181}
@@ -419,7 +442,9 @@ + (FMDatabase *)encryptOrUnencryptDb:(FMDatabase *)db
419442
420443+ (BOOL )verifyDatabaseAccess : (FMDatabase *)db error : (NSError **)error
421444{
422- if (![db goodConnection ]) {
445+ FMResultSet *rs = [db executeQuery: @" select name from sqlite_master where type='table'" ];
446+ if (rs == nil ) {
447+ // May not be results, but rs should never be nil coming back.
423448 if (error != nil ) {
424449 NSString *errorDesc = [NSString stringWithFormat: kSFSmartStoreVerifyReadDbErrorDesc , [db databasePath ], [db lastErrorMessage ]];
425450 *error = [NSError errorWithDomain: kSFSmartStoreDbErrorDomain
0 commit comments