diff --git a/README.md b/README.md index 302f1cd..603ef1c 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,50 @@ The Plugin is only for iOS. // failed }); +### Save multiple values at once in UserDefaults + +var suite = "group.cats.catsAreAwesome"; +var options1 = { + key: "foo", + value: "bar"}; + +var options2 = { + key: "foo2", + value: "bar2"}; + +window.AppGroupsUserDefaults.saveAll(suite, [options1, options2], + function() { + // success + }, function() { + // failed + }); + +### Remove from UserDefaults + +var options = { + key: "foo", + suite: "group.cats.catsAreAwesome"}; + +window.AppGroupsUserDefaults.remove(options, + function() { + // success + }, function() { + // failed + }); + +### Remove multiple values at once from UserDefaults + +var suite = "group.cats.catsAreAwesome"; +var key1 = "foo"; +var key2 = "foo2"; + +window.AppGroupsUserDefaults.removeAll(suite, [key1, key2], + function() { + // success + }, function() { + // failed + }); + ### Load from UserDefaults var options = { diff --git a/package.json b/package.json new file mode 100644 index 0000000..de6465b --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "cordova-plugin-nsuserdefaults-for-app-groups", + "version": "1.0.0", + "description": "Cordova plugin to interact fith NSUserDefaults", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/maksymilian-majer/cordova-plugin-nsuserdefaults-for-app-groups.git" + }, + "author": "", + "license": "UNLICENSED", + "bugs": { + "url": "https://github.com/maksymilian-majer/cordova-plugin-nsuserdefaults-for-app-groups/issues" + }, + "homepage": "https://github.com/maksymilian-majer/cordova-plugin-nsuserdefaults-for-app-groups#readme" +} diff --git a/src/ios/AppGroupsUserDefaults.m b/src/ios/AppGroupsUserDefaults.m index 706639e..a34da9d 100644 --- a/src/ios/AppGroupsUserDefaults.m +++ b/src/ios/AppGroupsUserDefaults.m @@ -28,6 +28,87 @@ - (void) save:(CDVInvokedUrlCommand*)command [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; } +- (void) saveAll:(CDVInvokedUrlCommand*)command +{ + // initialize user default' dictionary for the given app group (suite) + NSString* suite = [command.arguments firstObject]; + NSUserDefaults* prefs = [[NSUserDefaults alloc] initWithSuiteName:suite]; + + // loop through all command arguments + int i; + for (i = 1; i < command.arguments.count; i++) { + // load the options + NSDictionary *arguments = [command.arguments objectAtIndex:i]; + NSString* key = [arguments objectForKey:@"key"]; + NSString* value = [arguments objectForKey:@"value"]; + + // do the magic + [prefs setObject:value forKey:key]; + } + + // force the sync + [prefs synchronize]; + + // give the callback + CDVPluginResult* result = nil; + for (i = 1; i < command.arguments.count; i++) { + // load the options + NSDictionary *arguments = [command.arguments objectAtIndex:i]; + NSString* key = [arguments objectForKey:@"key"]; + NSString* value = [arguments objectForKey:@"value"]; + + BOOL valuesAreEqual = [[prefs stringForKey:key] isEqualToString:value]; + if(result == nil && valuesAreEqual) + { + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + } else if (!valuesAreEqual) { + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION]; + break; + } + } + + [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; +} + +- (void) remove:(CDVInvokedUrlCommand*)command +{ + // load the options + NSDictionary* arguments = [command.arguments objectAtIndex:0]; + NSString* key = [arguments objectForKey:@"key"]; + NSString* suite = [arguments objectForKey:@"suite"]; + + // do more magic + NSUserDefaults *prefs = [[NSUserDefaults alloc] initWithSuiteName:suite]; + [prefs removeObjectForKey:key]; + + // force the sync + [prefs synchronize]; + + [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId]; +} + +- (void) removeAll:(CDVInvokedUrlCommand*)command +{ + // initialize user default' dictionary for the given app group (suite) + NSString* suite = [command.arguments firstObject]; + NSUserDefaults* prefs = [[NSUserDefaults alloc] initWithSuiteName:suite]; + + // loop through all command arguments + int i; + for (i = 1; i < command.arguments.count; i++) { + // load the key + NSString* key = [command.arguments objectAtIndex:i]; + + // do the magic + [prefs removeObjectForKey:key]; + } + + // force the sync + [prefs synchronize]; + + [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId]; +} + - (void) load:(CDVInvokedUrlCommand*)command { // load the options diff --git a/www/app_groups_user_defaults.js b/www/app_groups_user_defaults.js index 71cc50d..4107911 100644 --- a/www/app_groups_user_defaults.js +++ b/www/app_groups_user_defaults.js @@ -8,5 +8,19 @@ AppGroupsUserDefaults.prototype.load = function(options, success, fail) { cordova.exec(success, fail, "AppGroupsUserDefaults", "load", [options]); }; +AppGroupsUserDefaults.prototype.remove = function(options, success, fail) { + cordova.exec(success, fail, "AppGroupsUserDefaults", "remove", [options]); +}; + +AppGroupsUserDefaults.prototype.saveAll = function(suite, options, success, fail) { + var args = [suite].concat(options); + cordova.exec(success ,fail , "AppGroupsUserDefaults", "saveAll", args); +}; + +AppGroupsUserDefaults.prototype.removeAll = function(suite, options, success, fail) { + var args = [suite].concat(options); + cordova.exec(success, fail, "AppGroupsUserDefaults", "removeAll", args); +}; + var appGroupsUserDefaults = new AppGroupsUserDefaults(); module.exports = appGroupsUserDefaults;