Skip to content

Instantly share code, notes, and snippets.

@paularmstrong
Created June 30, 2011 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paularmstrong/1055369 to your computer and use it in GitHub Desktop.
Save paularmstrong/1055369 to your computer and use it in GitHub Desktop.
Bridge for iOSImpact to interact with NSUserDefaults
//
// JS_UserDefaults.h
//
// Created by Paul Armstrong on 6/28/11.
// Copyright 2011 Paul Armstrong Designs. All rights reserved.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "JS_BaseClass.h"
@interface JS_UserDefaults : JS_BaseClass
@end
//
// JS_UserDefaults.m
//
// Created by Paul Armstrong on 6/28/11.
// Copyright 2011 Paul Armstrong Designs. All rights reserved.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "JS_UserDefaults.h"
@implementation JS_UserDefaults
JS_FUNC(JS_UserDefaults, get, ctx, argc, argv)
{
if (argc > 1 || argc == 0)
{
return NULL;
}
id value = [[NSUserDefaults standardUserDefaults] valueForKey:JSValueToNSString(ctx, argv[0])];
NSString *valueClass = [NSString stringWithFormat:@"%@", [value class]];
if ([valueClass isEqualToString:@"NSCFBoolean"])
{
return JSValueMakeBoolean(ctx, [value boolValue]);
}
else if ([valueClass isEqualToString:@"NSCFNumber"])
{
return JSValueMakeNumber(ctx, [value doubleValue]);
}
else
{
JSStringRef str = JSStringCreateWithUTF8CString([[NSString stringWithFormat:@"%@", value] UTF8String]);
JSValueRef ret = JSValueMakeString(ctx, str);
JSStringRelease(str);
return ret;
}
}
JS_FUNC(JS_UserDefaults, set, ctx, argc, argv)
{
if (argc != 2)
{
return NULL;
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
switch ((int)JSValueGetType(ctx, argv[1]))
{
case kJSTypeBoolean:
case kJSTypeUndefined:
case kJSTypeNull:
[defaults setBool:JSValueToBoolean(ctx, argv[1]) forKey:JSValueToNSString(ctx, argv[0])];
break;
case kJSTypeNumber:
[defaults setInteger:JSValueToNumber(ctx, argv[1], NULL) forKey:JSValueToNSString(ctx, argv[0])];
break;
default:
[defaults setValue:JSValueToNSString(ctx, argv[1]) forKey:JSValueToNSString(ctx, argv[0])];
break;
}
return NULL;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment