Skip to content
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
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
81 changes: 81 additions & 0 deletions src/ios/AppGroupsUserDefaults.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions www/app_groups_user_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;