- Remember user settings on app update ios-develop
authorantonc27 <antonc27@mail.ru>
Sun, 11 Mar 2018 23:24:05 +0100
branchios-develop
changeset 13172 c129707eb1e6
parent 13171 ba5c794adae3
child 13196 0c1f3a8ba0bb
- Remember user settings on app update P.S. Ideally must not erase user's saves and schemes too...
project_files/HedgewarsMobile/Classes/CreationChamber.m
project_files/HedgewarsMobile/Classes/ExtraCategories.h
project_files/HedgewarsMobile/Classes/ExtraCategories.m
--- a/project_files/HedgewarsMobile/Classes/CreationChamber.m	Sun Mar 11 22:39:40 2018 +0100
+++ b/project_files/HedgewarsMobile/Classes/CreationChamber.m	Sun Mar 11 23:24:05 2018 +0100
@@ -72,16 +72,15 @@
 #pragma mark Settings
 + (void)createSettings {
     NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
-    [settings setObject:[NSNumber numberWithBool:NO] forKey:@"alternate"];
-    [settings setObject:[NSNumber numberWithBool:YES] forKey:@"music"];
-    [settings setObject:[NSNumber numberWithBool:YES] forKey:@"sound"];
-    [settings setObject:[NSNumber numberWithBool:YES] forKey:@"sync_ws"];
+    
+    [settings setDefaultBool:NO forNonExistingKey:@"alternate"];
+    [settings setDefaultBool:YES forNonExistingKey:@"music"];
+    [settings setDefaultBool:YES forNonExistingKey:@"sound"];
+    [settings setDefaultBool:YES forNonExistingKey:@"sync_ws"];
 
     // don't overwrite these two strings when present
-    if ([settings objectForKey:@"username"] == nil)
-        [settings setObject:@"" forKey:@"username"];
-    if ([settings objectForKey:@"password"] == nil)
-        [settings setObject:@"" forKey:@"password"];
+    [settings setDefaultValue:@"" forNonExistingKey:@"username"];
+    [settings setDefaultValue:@"" forNonExistingKey:@"password"];
 
     [settings synchronize];
 }
--- a/project_files/HedgewarsMobile/Classes/ExtraCategories.h	Sun Mar 11 22:39:40 2018 +0100
+++ b/project_files/HedgewarsMobile/Classes/ExtraCategories.h	Sun Mar 11 23:24:05 2018 +0100
@@ -72,3 +72,9 @@
 
 @end
 
+@interface NSUserDefaults (setNonExisting)
+
+- (void)setDefaultValue:(nullable id)value forNonExistingKey:(NSString *_Nonnull)key;
+- (void)setDefaultBool:(BOOL)boolValue forNonExistingKey:(NSString *_Nonnull)key;
+
+@end
--- a/project_files/HedgewarsMobile/Classes/ExtraCategories.m	Sun Mar 11 22:39:40 2018 +0100
+++ b/project_files/HedgewarsMobile/Classes/ExtraCategories.m	Sun Mar 11 23:24:05 2018 +0100
@@ -190,3 +190,19 @@
 }
 
 @end
+
+
+#pragma mark -
+@implementation NSUserDefaults (setNonExisting)
+
+- (void)setDefaultValue:(nullable id)value forNonExistingKey:(NSString *_Nonnull)key {
+    if ([self objectForKey:key] == nil) {
+        [self setObject:value forKey:key];
+    }
+}
+
+- (void)setDefaultBool:(BOOL)boolValue forNonExistingKey:(NSString *_Nonnull)key {
+    [self setDefaultValue:[NSNumber numberWithBool:boolValue] forNonExistingKey:key];
+}
+
+@end