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

Add possibility to use a shared app group directory #879

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,14 @@ where the `iosDatabaseLocation` option may be set to one of the following choice
- `Library`: `Library` subdirectory - backed up by iCloud, *NOT* visible to iTunes
- `Documents`: `Documents` subdirectory - visible to iTunes and backed up by iCloud

Another option for **iOS** is this:

```js
var db = window.sqlitePlugin.openDatabase({name: 'my.db', iosDirectoryURL: 'Library'}, successcb, errorcb);
```

where `iosDirectoryURL` is a directory path for shared app groups like `/private/var/mobile/Containers/Shared/AppGroup/XXX-ZZZ...`.

**WARNING:** Again, the new "default" iosDatabaseLocation value is *NOT* the same as the old default location and would break an upgrade for an app using the old default value (0) on iOS.

DEPRECATED ALTERNATIVE to be removed in September 2018:
Expand Down
17 changes: 17 additions & 0 deletions src/ios/SQLitePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ -(id) getDBPath:(NSString *)dbFile at:(NSString *)atkey {
return dbPath;
}

-(id) getDBPath:(NSString *)dbFile inDirectory:(NSString *)directory {
if (dbFile == NULL || directory == NULL) {
return NULL;
}

NSString *dbPath = [directory stringByAppendingPathComponent: dbFile];
return dbPath;
}

-(void)echoStringValue: (CDVInvokedUrlCommand*)command
{
CDVPluginResult * pluginResult = nil;
Expand Down Expand Up @@ -115,6 +124,10 @@ -(void)openNow: (CDVInvokedUrlCommand*)command
// DLog(@"using db location: %@", dblocation);

NSString *dbname = [self getDBPath:dbfilename at:dblocation];
NSString *directoryURL = [options objectForKey:@"iosDirectoryURL"];
if (directoryURL != NULL) {
dbname = [self getDBPath:dbfilename inDirectory:directoryURL];
}

if (!sqlite3_threadsafe()) {
// INTERNAL PLUGIN ERROR:
Expand Down Expand Up @@ -238,6 +251,10 @@ -(void)deleteNow: (CDVInvokedUrlCommand*)command
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"INTERNAL PLUGIN ERROR: You must specify database path"];
} else {
NSString *dbPath = [self getDBPath:dbFileName at:dblocation];
NSString *directoryURL = [options objectForKey:@"iosDirectoryURL"];
if (directoryURL != NULL) {
dbPath = [self getDBPath:dbFileName inDirectory:directoryURL];
}

if ([[NSFileManager defaultManager]fileExistsAtPath:dbPath]) {
DLog(@"delete full db path: %@", dbPath);
Expand Down
36 changes: 21 additions & 15 deletions www/SQLitePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,19 +568,21 @@
if (!openargs.name) {
throw newSQLError('Database name value is missing in openDatabase call');
}
if (!openargs.iosDatabaseLocation && !openargs.location && openargs.location !== 0) {
throw newSQLError('Database location or iosDatabaseLocation setting is now mandatory in openDatabase call.');
if (!openargs.iosDatabaseLocation && !openargs.location && openargs.location !== 0 && !openargs.iosDirectoryURL) {
throw newSQLError('Database location, iosDatabaseLocation or iosDirectoryURL setting is now mandatory in openDatabase call.');
}
if (!!openargs.location && !!openargs.iosDatabaseLocation) {
throw newSQLError('AMBIGUOUS: both location and iosDatabaseLocation settings are present in openDatabase call. Please use either setting, not both.');
}
dblocation = !!openargs.location && openargs.location === 'default' ? iosLocationMap['default'] : !!openargs.iosDatabaseLocation ? iosLocationMap[openargs.iosDatabaseLocation] : dblocations[openargs.location];
if (!dblocation) {
throw newSQLError('Valid iOS database location could not be determined in openDatabase call');
}
openargs.dblocation = dblocation;
if (!!openargs.createFromLocation && openargs.createFromLocation === 1) {
openargs.createFromResource = "1";
if (!openargs.iosDirectoryURL) {
dblocation = !!openargs.location && openargs.location === 'default' ? iosLocationMap['default'] : !!openargs.iosDatabaseLocation ? iosLocationMap[openargs.iosDatabaseLocation] : dblocations[openargs.location];
if (!dblocation) {
throw newSQLError('Valid iOS database location could not be determined in openDatabase call');
}
openargs.dblocation = dblocation;
if (!!openargs.createFromLocation && openargs.createFromLocation === 1) {
openargs.createFromResource = "1";
}
}
if (!!openargs.androidDatabaseProvider && !!openargs.androidDatabaseImplementation) {
throw newSQLError('AMBIGUOUS: both androidDatabaseProvider and deprecated androidDatabaseImplementation settings are present in openDatabase call. Please drop androidDatabaseImplementation in favor of androidDatabaseProvider.');
Expand Down Expand Up @@ -622,17 +624,21 @@
}
args.path = dbname;
}
if (!first.iosDatabaseLocation && !first.location && first.location !== 0) {
throw newSQLError('Database location or iosDatabaseLocation setting is now mandatory in deleteDatabase call.');
if (!first.iosDatabaseLocation && !first.location && first.location !== 0 && !first.iosDirectoryURL) {
throw newSQLError('Database location, iosDatabaseLocation or iosDirectoryURL setting is now mandatory in deleteDatabase call.');
}
if (!!first.location && !!first.iosDatabaseLocation) {
throw newSQLError('AMBIGUOUS: both location and iosDatabaseLocation settings are present in deleteDatabase call. Please use either setting value, not both.');
}
dblocation = !!first.location && first.location === 'default' ? iosLocationMap['default'] : !!first.iosDatabaseLocation ? iosLocationMap[first.iosDatabaseLocation] : dblocations[first.location];
if (!dblocation) {
throw newSQLError('Valid iOS database location could not be determined in deleteDatabase call');
if (!first.iosDirectoryURL) {
dblocation = !!first.location && first.location === 'default' ? iosLocationMap['default'] : !!first.iosDatabaseLocation ? iosLocationMap[first.iosDatabaseLocation] : dblocations[first.location];
if (!dblocation) {
throw newSQLError('Valid iOS database location could not be determined in deleteDatabase call');
}
args.dblocation = dblocation;
} else {
args.iosDirectoryURL = first.iosDirectoryURL;
}
args.dblocation = dblocation;
delete SQLitePlugin.prototype.openDBs[args.path];
return cordova.exec(success, error, "SQLitePlugin", "delete", [args]);
}
Expand Down