From 064183c2cab1874c011aeb4e8f3ed3733477eaf8 Mon Sep 17 00:00:00 2001 From: Andreas Bissinger Date: Fri, 14 Jun 2019 12:58:37 +0200 Subject: [PATCH 1/2] Enable path from shared app group (issue #858) --- README.md | 8 ++++++++ src/ios/SQLitePlugin.m | 17 +++++++++++++++++ www/SQLitePlugin.js | 36 +++++++++++++++++++++--------------- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index d75b7e2c3..2d79209ce 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/ios/SQLitePlugin.m b/src/ios/SQLitePlugin.m index dbe4e9580..442090b41 100755 --- a/src/ios/SQLitePlugin.m +++ b/src/ios/SQLitePlugin.m @@ -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; @@ -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: @@ -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); diff --git a/www/SQLitePlugin.js b/www/SQLitePlugin.js index 8b48865d8..750b05b28 100644 --- a/www/SQLitePlugin.js +++ b/www/SQLitePlugin.js @@ -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.'); @@ -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 = iosDirectoryURL; } - args.dblocation = dblocation; delete SQLitePlugin.prototype.openDBs[args.path]; return cordova.exec(success, error, "SQLitePlugin", "delete", [args]); } From ae1beac6391eef1895a48773286ce13ab651ce57 Mon Sep 17 00:00:00 2001 From: Andreas Bissinger Date: Fri, 14 Jun 2019 14:37:46 +0200 Subject: [PATCH 2/2] fix: use correct variable --- www/SQLitePlugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/SQLitePlugin.js b/www/SQLitePlugin.js index 750b05b28..7aa0b960c 100644 --- a/www/SQLitePlugin.js +++ b/www/SQLitePlugin.js @@ -637,7 +637,7 @@ } args.dblocation = dblocation; } else { - args.iosDirectoryURL = iosDirectoryURL; + args.iosDirectoryURL = first.iosDirectoryURL; } delete SQLitePlugin.prototype.openDBs[args.path]; return cordova.exec(success, error, "SQLitePlugin", "delete", [args]);