AppPreferences.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //
  2. // AppPreferences.m
  3. //
  4. //
  5. // Created by Tue Topholm on 31/01/11.
  6. // Copyright 2011 Sugee. All rights reserved.
  7. //
  8. // Modified by Ivan Baktsheev, 2012-2016
  9. //
  10. // THIS HAVEN'T BEEN TESTED WITH CHILD PANELS YET.
  11. #import "AppPreferences.h"
  12. @implementation AppPreferences
  13. - (void)pluginInitialize
  14. {
  15. }
  16. // http://useyourloaf.com/blog/sync-preference-data-with-icloud/
  17. - (void)defaultsChanged:(NSNotification *)notification {
  18. NSString * jsCallBack = [NSString stringWithFormat:@"cordova.fireDocumentEvent('preferencesChanged');"];
  19. // if ([notification.name isEqualToString:NSUserDefaultsDidChangeNotification])
  20. // else
  21. if ([notification.name isEqualToString:NSUbiquitousKeyValueStoreDidChangeExternallyNotification]) {
  22. NSNumber *changeReasonNumber = notification.userInfo[NSUbiquitousKeyValueStoreChangeReasonKey];
  23. if (changeReasonNumber) {
  24. NSInteger changeReason = [changeReasonNumber intValue];
  25. // preference store can be synchronized with cloud
  26. // Good sync example: https://github.com/Relfos/TERRA-Engine/blob/7ef17e6b67968a40212fbb678135af0000246097/Engine/OS/iOS/ObjectiveC/TERRA_iCloudSync.m
  27. // Another one: http://useyourloaf.com/blog/sync-preference-data-with-icloud/
  28. /*
  29. if (changeReason == NSUbiquitousKeyValueStoreServerChange || changeReason == NSUbiquitousKeyValueStoreInitialSyncChange || changeReason == NSUbiquitousKeyValueStoreAccountChange) {
  30. //id localStore = [self _storeForLocation:CQSettingsLocationDevice];
  31. //id cloudStore = [self _storeForLocation:CQSettingsLocationCloud];
  32. for (NSString *key in notification.userInfo[NSUbiquitousKeyValueStoreChangedKeysKey])
  33. localStore[key] = cloudStore[key];
  34. }
  35. */
  36. }
  37. }
  38. // https://github.com/EddyVerbruggen/cordova-plugin-3dtouch/blob/master/src/ios/app/AppDelegate+threedeetouch.m
  39. if ([self.webView respondsToSelector:@selector(stringByEvaluatingJavaScriptFromString:)]) {
  40. // UIWebView
  41. [self.webView performSelectorOnMainThread:@selector(stringByEvaluatingJavaScriptFromString:) withObject:jsCallBack waitUntilDone:NO];
  42. } else if ([self.webView respondsToSelector:@selector(evaluateJavaScript:completionHandler:)]) {
  43. // WKWebView
  44. [self.webView performSelector:@selector(evaluateJavaScript:completionHandler:) withObject:jsCallBack withObject:nil];
  45. } else {
  46. NSLog(@"No compatible method found to send notification to the webview. Please notify the plugin author.");
  47. }
  48. }
  49. - (void)watch:(CDVInvokedUrlCommand*)command
  50. {
  51. __block CDVPluginResult* result = nil;
  52. NSDictionary* options = [self validateOptions:command];
  53. if (!options)
  54. return;
  55. bool watchChanges = true;
  56. NSNumber *subscribe = [options objectForKey:@"subscribe"];
  57. if (subscribe != nil) {
  58. watchChanges = [subscribe boolValue];
  59. }
  60. if (watchChanges) {
  61. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(defaultsChanged:) name:NSUserDefaultsDidChangeNotification object:nil];
  62. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(defaultsChanged:) name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:nil];
  63. } else {
  64. [[NSNotificationCenter defaultCenter] removeObserver:self name:NSUserDefaultsDidChangeNotification object:nil];
  65. [[NSNotificationCenter defaultCenter] removeObserver:self name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:nil];
  66. }
  67. [self.commandDelegate runInBackground:^{
  68. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
  69. }];
  70. }
  71. - (NSDictionary*)validateOptions:(CDVInvokedUrlCommand*)command
  72. {
  73. NSDictionary* options = [[command arguments] objectAtIndex:0];
  74. if (!options) {
  75. CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"no options given"];
  76. [self.commandDelegate sendPluginResult:result callbackId:[command callbackId]];
  77. return nil;
  78. }
  79. return options;
  80. }
  81. - (id)getStoreForOptions:(NSDictionary*)options
  82. {
  83. NSString *suiteName = [options objectForKey:@"suiteName"];
  84. NSString *cloudSync = [options objectForKey:@"cloudSync"];
  85. id dataStore = nil;
  86. if (suiteName != nil && ![@"" isEqualToString:suiteName]) {
  87. dataStore = [[NSUserDefaults alloc] initWithSuiteName:suiteName];
  88. } else if (cloudSync != nil) {
  89. dataStore = [NSUbiquitousKeyValueStore defaultStore];
  90. } else {
  91. dataStore = [NSUserDefaults standardUserDefaults];
  92. }
  93. return dataStore;
  94. }
  95. - (void)fetch:(CDVInvokedUrlCommand*)command
  96. {
  97. __block CDVPluginResult* result = nil;
  98. NSDictionary* options = [self validateOptions:command];
  99. if (!options)
  100. return;
  101. NSString *settingsDict = [options objectForKey:@"dict"];
  102. NSString *settingsName = [options objectForKey:@"key"];
  103. id dataStore = [self getStoreForOptions:options];
  104. __block id target = dataStore;
  105. [self.commandDelegate runInBackground:^{
  106. // NSMutableDictionary *mutable = [[dict mutableCopy] autorelease];
  107. // NSDictionary *dict = [[mutable copy] autorelease];
  108. @try {
  109. NSString *returnVar;
  110. id settingsValue = nil;
  111. if (settingsDict) {
  112. target = [dataStore dictionaryForKey:settingsDict];
  113. if (target == nil) {
  114. returnVar = nil;
  115. }
  116. }
  117. if (target != nil) {
  118. settingsValue = [target objectForKey:settingsName];
  119. }
  120. if (settingsValue != nil) {
  121. if ([settingsValue isKindOfClass:[NSString class]]) {
  122. NSString *escaped = [(NSString*)settingsValue stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
  123. escaped = [escaped stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
  124. returnVar = [NSString stringWithFormat:@"\"%@\"", escaped];
  125. } else if ([settingsValue isKindOfClass:[NSNumber class]]) {
  126. if ((NSNumber*)settingsValue == (void*)kCFBooleanFalse || (NSNumber*)settingsValue == (void*)kCFBooleanTrue) {
  127. // const char * x = [(NSNumber*)settingsValue objCType];
  128. // NSLog(@"boolean %@", [(NSNumber*)settingsValue boolValue] == NO ? @"false" : @"true");
  129. returnVar = [NSString stringWithFormat:@"%@", [(NSNumber*)settingsValue boolValue] == YES ? @"true": @"false"];
  130. } else {
  131. // TODO: int, float
  132. // NSLog(@"number");
  133. returnVar = [NSString stringWithFormat:@"%@", (NSNumber*)settingsValue];
  134. }
  135. } else if ([settingsValue isKindOfClass:[NSData class]]) { // NSData
  136. returnVar = [[NSString alloc] initWithData:(NSData*)settingsValue encoding:NSUTF8StringEncoding];
  137. }
  138. } else {
  139. // TODO: also submit dict
  140. returnVar = [self getSettingFromBundle:settingsName]; //Parsing Root.plist
  141. // if (returnVar == nil)
  142. // @throw [NSException exceptionWithName:nil reason:@"Key not found" userInfo:nil];;
  143. }
  144. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:returnVar];
  145. } @catch (NSException * e) {
  146. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT messageAsString:[e reason]];
  147. } @finally {
  148. [self.commandDelegate sendPluginResult:result callbackId:[command callbackId]];
  149. }
  150. }];
  151. }
  152. - (void)remove:(CDVInvokedUrlCommand*)command
  153. {
  154. __block CDVPluginResult* result = nil;
  155. NSDictionary* options = [self validateOptions:command];
  156. if (!options)
  157. return;
  158. NSString *settingsDict = [options objectForKey:@"dict"];
  159. NSString *settingsName = [options objectForKey:@"key"];
  160. id dataStore = [self getStoreForOptions:options];
  161. __block id target = dataStore;
  162. //[self.commandDelegate runInBackground:^{
  163. @try {
  164. NSString *returnVar;
  165. if (settingsDict) {
  166. target = [dataStore dictionaryForKey:settingsDict];
  167. if (target)
  168. target = [target mutableCopy];
  169. }
  170. if (target != nil) {
  171. [target removeObjectForKey:settingsName];
  172. if (target != dataStore)
  173. [dataStore setObject:(NSMutableDictionary*)target forKey:settingsDict];
  174. [dataStore synchronize];
  175. }
  176. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:returnVar];
  177. } @catch (NSException * e) {
  178. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT messageAsString:[e reason]];
  179. } @finally {
  180. [self.commandDelegate sendPluginResult:result callbackId:[command callbackId]];
  181. }
  182. //}];
  183. }
  184. - (void)clearAll:(CDVInvokedUrlCommand*)command
  185. {
  186. __block CDVPluginResult* result = nil;
  187. NSDictionary* options = [self validateOptions:command];
  188. if (!options)
  189. return;
  190. NSString *settingsDict = [options objectForKey:@"dict"];
  191. NSString *suiteName = [options objectForKey:@"suiteName"];
  192. NSString *cloudSync = [options objectForKey:@"cloudSync"];
  193. id dataStore = [self getStoreForOptions:options];
  194. __block id target = dataStore;
  195. //[self.commandDelegate runInBackground:^{
  196. @try {
  197. NSString *appDomain;
  198. if (suiteName != nil) {
  199. appDomain = suiteName;
  200. [dataStore removePersistentDomainForName:appDomain];
  201. } else if (cloudSync) {
  202. for (NSString *key in [dataStore allKeys]) {
  203. [dataStore removeObjectForKey:key];
  204. }
  205. } else {
  206. appDomain = [[NSBundle mainBundle] bundleIdentifier];
  207. [dataStore removePersistentDomainForName:appDomain];
  208. }
  209. [dataStore synchronize];
  210. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
  211. } @catch (NSException * e) {
  212. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT messageAsString:[e reason]];
  213. } @finally {
  214. [self.commandDelegate sendPluginResult:result callbackId:[command callbackId]];
  215. }
  216. //}];
  217. }
  218. - (void)show:(CDVInvokedUrlCommand*)command
  219. {
  220. __block CDVPluginResult* result;
  221. if(&UIApplicationOpenSettingsURLString != nil) {
  222. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
  223. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
  224. } else {
  225. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"switching to preferences not supported"];
  226. }
  227. [self.commandDelegate sendPluginResult:result callbackId:[command callbackId]];
  228. }
  229. - (void)store:(CDVInvokedUrlCommand*)command
  230. {
  231. __block CDVPluginResult* result;
  232. NSDictionary* options = [self validateOptions:command];
  233. if (!options)
  234. return;
  235. NSString *settingsDict = [options objectForKey:@"dict"];
  236. NSString *settingsName = [options objectForKey:@"key"];
  237. NSString *settingsValue = [options objectForKey:@"value"];
  238. NSString *settingsType = [options objectForKey:@"type"];
  239. // NSLog(@"%@ = %@ (%@)", settingsName, settingsValue, settingsType);
  240. //[self.commandDelegate runInBackground:^{
  241. id dataStore = [self getStoreForOptions:options];
  242. id target = dataStore;
  243. // NSMutableDictionary *mutable = [[dict mutableCopy] autorelease];
  244. // NSDictionary *dict = [[mutable copy] autorelease];
  245. if (settingsDict) {
  246. target = [[dataStore dictionaryForKey:settingsDict] mutableCopy];
  247. if (!target) {
  248. target = [[NSMutableDictionary alloc] init];
  249. #if !__has_feature(objc_arc)
  250. [target autorelease];
  251. #endif
  252. }
  253. }
  254. NSError* error = nil;
  255. id JSONObj = [NSJSONSerialization
  256. JSONObjectWithData:[settingsValue dataUsingEncoding:NSUTF8StringEncoding]
  257. options:NSJSONReadingAllowFragments
  258. error:&error
  259. ];
  260. if (error != nil) {
  261. NSLog(@"NSString JSONObject error: %@", [error localizedDescription]);
  262. }
  263. @try {
  264. if ([settingsType isEqual: @"string"] && [JSONObj isKindOfClass:[NSString class]]) {
  265. [target setObject:(NSString*)JSONObj forKey:settingsName];
  266. } else if ([settingsType isEqual: @"number"] && [JSONObj isKindOfClass:[NSNumber class]]) {
  267. [target setObject:(NSNumber*)JSONObj forKey:settingsName];
  268. // setInteger: forKey, setFloat: forKey:
  269. } else if ([settingsType isEqual: @"boolean"]) {
  270. [target setObject:JSONObj forKey:settingsName];
  271. } else {
  272. // data
  273. [target setObject:[settingsValue dataUsingEncoding:NSUTF8StringEncoding] forKey:settingsName];
  274. }
  275. if (target != dataStore)
  276. [dataStore setObject:(NSMutableDictionary*)target forKey:settingsDict];
  277. [dataStore synchronize];
  278. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
  279. } @catch (NSException * e) {
  280. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT messageAsString:[e reason]];
  281. } @finally {
  282. [self.commandDelegate sendPluginResult:result callbackId:[command callbackId]];
  283. }
  284. //}];
  285. }
  286. /*
  287. Parsing the Root.plist for the key, because there is a bug/feature in Settings.bundle
  288. So if the user haven't entered the Settings for the app, the default values aren't accessible through NSUserDefaults.
  289. */
  290. - (NSString*)getSettingFromBundle:(NSString*)settingsName
  291. {
  292. NSString *pathStr = [[NSBundle mainBundle] bundlePath];
  293. NSString *settingsBundlePath = [pathStr stringByAppendingPathComponent:@"Settings.bundle"];
  294. NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"Root.plist"];
  295. NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];
  296. NSArray *prefSpecifierArray = [settingsDict objectForKey:@"PreferenceSpecifiers"];
  297. NSDictionary *prefItem;
  298. for (prefItem in prefSpecifierArray)
  299. {
  300. if ([[prefItem objectForKey:@"Key"] isEqualToString:settingsName])
  301. return [prefItem objectForKey:@"DefaultValue"];
  302. }
  303. return nil;
  304. }
  305. @end