Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support to enabling database when ios is locked #264

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/ios/SQLitePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ -(id) getDBPath:(NSString *)dbFile at:(NSString *)atkey {
return dbPath;
}

-(void)enableDatabaseWhenLocked:(BOOL)enable path:(NSString*)path{
NSDictionary *attributes = @{NSFileProtectionKey: enable ? NSFileProtectionNone : NSFileProtectionComplete};
NSError *error;
if(![[NSFileManager defaultManager] setAttributes:attributes
ofItemAtPath:path
error:&error]){
NSLog(@"DATABASE WARNING! Database access was not enabled because of an error: %@", [error localizedDescription]);
}
else{
NSLog(@"Database access enabled when locked.");
}
}

-(void)open: (CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
Expand All @@ -122,13 +135,15 @@ -(void)open: (CDVInvokedUrlCommand*)command

NSString *dbname = [self getDBPath:dbfilename at:dblocation];

NSNumber *enableDatabaseWhenLocked = [options valueForKey:@"enableDatabaseWhenLocked"];

if (dbname == NULL) {
NSLog(@"No db name specified for open");
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"You must specify database name"];
}
else {
NSValue *dbPointer = [openDBs objectForKey:dbfilename];

if (dbPointer != NULL) {
NSLog(@"Reusing existing database connection for db name %@", dbfilename);
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Database opened"];
Expand All @@ -149,8 +164,12 @@ -(void)open: (CDVInvokedUrlCommand*)command
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Unable to open DB"];
return;
} else {
if(![enableDatabaseWhenLocked isEqual:[NSNull null]]){
[self enableDatabaseWhenLocked:[enableDatabaseWhenLocked boolValue] path:dbname];
}

sqlite3_create_function(db, "regexp", 2, SQLITE_ANY, NULL, &sqlite_regexp, NULL, NULL);

// for SQLCipher version:
// NSString *dbkey = [options objectForKey:@"key"];
// const char *key = NULL;
Expand All @@ -161,6 +180,7 @@ -(void)open: (CDVInvokedUrlCommand*)command
if(sqlite3_exec(db, (const char*)"SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
dbPointer = [NSValue valueWithPointer:db];
[openDBs setObject: dbPointer forKey: dbfilename];

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Database opened"];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Unable to open DB with key"];
Expand Down Expand Up @@ -194,10 +214,12 @@ -(void)createFromResource:(NSString *)dbfile withDbname:(NSString *)dbname {
NSError *error;
BOOL success = [[NSFileManager defaultManager] copyItemAtPath:prepopulatedDb toPath:dbname error:&error];

if(success)
if(success){
NSLog(@"Copied prepopulated DB content to: %@", dbname);
else
}
else{
NSLog(@"Unable to copy DB file: %@", [error localizedDescription]);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions www/SQLitePlugin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.