UserDefaults.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // UserDefaults.m
  3. // MacGap
  4. //
  5. // Created by Jeff Hanbury on 16/04/2014.
  6. // Copyright (c) 2014 Twitter. All rights reserved.
  7. //
  8. #import "UserDefaults.h"
  9. #import "JSEventHelper.h"
  10. @interface UserDefaults() {
  11. }
  12. -(void) setupNotificationCenter;
  13. @end
  14. @implementation UserDefaults
  15. - (id) initWithWebView:(WebView *) view{
  16. self = [super init];
  17. if (self) {
  18. self.webView = view;
  19. [self setupNotificationCenter];
  20. }
  21. return self;
  22. }
  23. -(void) setupNotificationCenter{
  24. [[NSNotificationCenter defaultCenter] addObserver:self
  25. selector:@selector(defaultsChanged:)
  26. name:NSUserDefaultsDidChangeNotification
  27. object:nil];
  28. }
  29. - (void)defaultsChanged:(NSNotification *)notification {
  30. NSDictionary* returnDict = [self myDefaultsDictionary];
  31. [JSEventHelper triggerEvent:@"userDefaultsChanged" withArgs:returnDict forWebView:self.webView];
  32. }
  33. - (NSString*) getMyDefaults {
  34. NSDictionary* myDefaults = [self myDefaultsDictionary];
  35. return [[Utils sharedInstance] convertDictionaryToJSON:myDefaults];
  36. }
  37. - (NSDictionary*) myDefaultsDictionary {
  38. NSString* prefix = [kWebScriptNamespace stringByAppendingString:@"_"];
  39. NSMutableDictionary* returnDict = [[NSMutableDictionary alloc] init];
  40. // Get the user defaults.
  41. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  42. // Build up a dictionary containing just the items beginning with our
  43. // prefix.
  44. for (NSString* key in [self getUserDefaultsKeys]) {
  45. if ([key hasPrefix:prefix]) {
  46. id val = [defaults valueForKey:key];
  47. [returnDict setObject:val forKey:key];
  48. }
  49. }
  50. return returnDict;
  51. }
  52. - (NSArray*) getUserDefaultsKeys {
  53. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  54. return [[prefs dictionaryRepresentation] allKeys];
  55. }
  56. - (void) removeObjectForKey:(NSString*)key {
  57. NSString* prefixedKey;
  58. prefixedKey = [self addPrefix:key];
  59. [[NSUserDefaults standardUserDefaults] removeObjectForKey:prefixedKey];
  60. [[NSUserDefaults standardUserDefaults] synchronize];
  61. }
  62. // Check we have a standard prefix for JS-modified keys, for security purposes.
  63. // If not, add it. This stops JavaScript from ever being able to modify keys
  64. // it did not create.
  65. - (NSString*) addPrefix:(NSString*)key {
  66. NSString* prefix;
  67. prefix = [kWebScriptNamespace stringByAppendingString:@"_"];
  68. if (![key hasPrefix:prefix]) {
  69. key = [prefix stringByAppendingString:key];
  70. }
  71. return key;
  72. }
  73. // String
  74. - (void) setString:(NSString*)key withValue:(NSString*)value {
  75. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  76. NSString* prefixedKey;
  77. prefixedKey = [self addPrefix:key];
  78. [prefs setObject:value forKey:prefixedKey];
  79. }
  80. - (NSString*) getString:(NSString *)key {
  81. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  82. return [prefs stringForKey:key];
  83. }
  84. // All the following must convert their type to NSNumber for JavaScript.
  85. // Integer
  86. - (void) setInteger:(NSString*)key withValue:(NSString*)value {
  87. NSString* prefixedKey;
  88. prefixedKey = [self addPrefix:key];
  89. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  90. NSInteger myInt = [value intValue];
  91. [prefs setInteger:myInt forKey:prefixedKey];
  92. }
  93. - (NSNumber*) getInteger:(NSString *)key {
  94. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  95. return [NSNumber numberWithInteger:[prefs integerForKey:key]];
  96. }
  97. // Boolean
  98. - (void) setBool:(NSString*)key withValue:(NSString*)value {
  99. NSString* prefixedKey;
  100. prefixedKey = [self addPrefix:key];
  101. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  102. BOOL myBool = [value boolValue];
  103. [prefs setBool:myBool forKey:prefixedKey];
  104. }
  105. - (NSNumber*) getBool:(NSString *)key {
  106. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  107. return [NSNumber numberWithBool:[prefs boolForKey:key]];
  108. }
  109. // Float
  110. - (void) setFloat:(NSString*)key withValue:(NSString*)value {
  111. NSString* prefixedKey;
  112. prefixedKey = [self addPrefix:key];
  113. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  114. float myFloat = [value floatValue];
  115. [prefs setFloat:myFloat forKey:prefixedKey];
  116. }
  117. - (NSNumber*) getFloat:(NSString *)key {
  118. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  119. return [NSNumber numberWithFloat:[prefs floatForKey:key]];
  120. }
  121. #pragma mark WebScripting Protocol
  122. + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector {
  123. return NO;
  124. }
  125. + (NSString*) webScriptNameForSelector:(SEL)selector {
  126. id result = nil;
  127. if (selector == @selector(getMyDefaults)) {
  128. result = @"getMyDefaults";
  129. }
  130. if (selector == @selector(removeObjectForKey:)) {
  131. result = @"removeObjectForKey";
  132. }
  133. else if (selector == @selector(setString:withValue:)) {
  134. result = @"setString";
  135. } else if (selector == @selector(getString:)) {
  136. result = @"getString";
  137. }
  138. else if (selector == @selector(setInteger:withValue:)) {
  139. result = @"setInteger";
  140. } else if (selector == @selector(getInteger:)) {
  141. result = @"getInteger";
  142. }
  143. else if (selector == @selector(setBool:withValue:)) {
  144. result = @"setBool";
  145. } else if (selector == @selector(getBool:)) {
  146. result = @"getBool";
  147. }
  148. else if (selector == @selector(setFloat:withValue:)) {
  149. result = @"setFloat";
  150. } else if (selector == @selector(getFloat:)) {
  151. result = @"getFloat";
  152. }
  153. return result;
  154. }
  155. + (BOOL) isKeyExcludedFromWebScript:(const char*)name {
  156. return NO;
  157. }
  158. @end