@@ -146,13 +146,36 @@ - (FMDatabase *)openStoreDatabaseWithName:(NSString *)storeName key:(NSString *)
146
146
return [[self class ] openDatabaseWithPath: fullDbFilePath key: key salt: salt error: error];
147
147
}
148
148
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
+
149
169
- (FMDatabaseQueue *)openStoreQueueWithName : (NSString *)storeName key : (NSString *)key salt : (NSString *)salt error : (NSError * __autoreleasing *)error {
150
170
171
+ [self fixFor12Bug: storeName key: key salt: salt];
172
+
151
173
__block BOOL result = YES ;
152
174
NSString *fullDbFilePath = [self fullDbFilePathForStoreName: storeName];
153
175
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath: fullDbFilePath];
154
176
[queue inDatabase: ^(FMDatabase* db) {
155
177
result = ([[self class ] setKeyForDb: db key: key salt: salt error: error] != nil );
178
+ result = result && [db goodConnection ]; // make sure SQLCipher is properly linked
156
179
}];
157
180
return (result ? queue : nil );
158
181
}
@@ -419,7 +442,9 @@ + (FMDatabase *)encryptOrUnencryptDb:(FMDatabase *)db
419
442
420
443
+ (BOOL )verifyDatabaseAccess : (FMDatabase *)db error : (NSError **)error
421
444
{
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.
423
448
if (error != nil ) {
424
449
NSString *errorDesc = [NSString stringWithFormat: kSFSmartStoreVerifyReadDbErrorDesc , [db databasePath ], [db lastErrorMessage ]];
425
450
*error = [NSError errorWithDomain: kSFSmartStoreDbErrorDomain
0 commit comments