Select Page

Working with (user) preferences is pretty easy in Objective c.
Preferences is just a phrase for persisting some info. If you need to save and load some small things, and you think a database is overkill, than user preferences might be just right.

Below a helper class that wraps access to some user preferences loading and saving. You can use the wrapper as a whole or just have a look at it to get the general idea of NSUserDefaults.
Please note that things saved through NSUserDefaults are NOT secure! So do not save things like passwords using NSUserDefaults.

The header file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//
//  UserDefaultsHelper.h
//
//  Copyright (c) 2012 mobiledev.nl. All rights reserved.
 
#import 
 
@interface UserDefaultsHelper : NSObject {
}
 
+(NSString*)getStringForKey:(NSString*)key;
+(NSInteger)getIntForkey:(NSString*)key;
+(NSDictionary*)getDictForKey:(NSString*)key;
+(NSArray*)getArrayForKey:(NSString*)key;
+(BOOL)getBoolForKey:(NSString*)key;
+(void)setStringForKey:(NSString*)value:(NSString*)key;
+(void)setIntForKey:(NSInteger)value:(NSString*)key;
+(void)setDictForKey:(NSDictionary*)value:(NSString*)key;
+(void)setArrayForKey:(NSArray*)value:(NSString*)key;
+(void)setBoolForKey:(BOOL)value:(NSString*)key;
 
@end

The implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
//  UserDefaultsHelper.m
//
//  Copyright (c) 2012 mobiledev.nl. All rights reserved.
 
#import "UserDefaultsHelper.h"
 
@implementation UserDefaultsHelper
 
+(NSString*)getStringForKey:(NSString*)key
{
  NSString* val = @"";
  NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
  if (standardUserDefaults) val = [standardUserDefaults stringForKey:key];
  if (val == NULL) val = @"";
  return val;
}
 
+(NSInteger)getIntForkey:(NSString *)key
{
  NSInteger val = 0;
  NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
  if (standardUserDefaults) val = [standardUserDefaults integerForKey:key];
  return val;
}
 
+(NSDictionary*)getDictForKey:(NSString*)key
{
  NSDictionary* val = nil;
  NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
  if (standardUserDefaults) val = [standardUserDefaults dictionaryForKey:key];
  return val;
}
 
+(NSArray*)getArrayForKey:(NSString*)key
{
  NSArray* val = nil;
  NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
  if (standardUserDefaults) val = [standardUserDefaults arrayForKey:key];
  return val;
}
 
+(BOOL)getBoolForKey:(NSString*)key
{
  BOOL val = FALSE;
  NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
  if (standardUserDefaults) val = [standardUserDefaults boolForKey:key];
  return val;
}
 
+(void)setStringForKey:(NSString*)value:(NSString*)key
{
	NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
	if (standardUserDefaults) 
  {
		[standardUserDefaults setObject:value forKey:key];
		[standardUserDefaults synchronize];
	}
}
 
+(void)setIntForKey:(NSInteger)value:(NSString*)key
{
	NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
	if (standardUserDefaults) 
  {
		[standardUserDefaults setInteger:value forKey:key];
		[standardUserDefaults synchronize];
	}
}
 
+(void)setDictForKey:(NSDictionary*)value:(NSString*)key
{
	NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
	if (standardUserDefaults) 
  {
		[standardUserDefaults setObject:value forKey:key];
		[standardUserDefaults synchronize];
	}
}
 
+(void)setArrayForKey:(NSArray*)value:(NSString*)key
{
	NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
	if (standardUserDefaults) 
  {
		[standardUserDefaults setObject:value forKey:key];
		[standardUserDefaults synchronize];
	}
}
 
+(void)setBoolForKey:(BOOL)value:(NSString*)key
{
	NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
	if (standardUserDefaults) 
  {
		[standardUserDefaults setBool:value forKey:key];
		[standardUserDefaults synchronize];
	}
}
 
@end