Last active
December 19, 2015 04:09
-
-
Save gpinigin/5895129 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface PassCodeSettings: NSObject<NSCoding> | |
@property (nonatomic, assign) BOOL isPasscodeEnabled; | |
@property (nonatomic, assign) NSUInteger attemptCount; | |
@property (nonatomic, assign) NSTimeInterval checkInterval; | |
@property (nonatomic, strong) NSDate *nextCheckDate; | |
@end | |
@interface InAppSettings : NSObject | |
@property (nonatomic, strong) NSString *applicationVersion; | |
@property (nonatomic, strong, readonly) PassCodeSettings *passCodeSettings; | |
+ (instancetype)defaultSettings; | |
- (void)savePasscodeSettings; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "InAppSettings.h" | |
NSString *const kApplicationVersion = @"kApplicationVersion"; | |
NSString *const kPasscodeSettings = @"kPasscodeSettings"; | |
@implementation InAppSettings | |
- (id)init { | |
self = [super init]; | |
if (self) { | |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | |
_applicationVersion = [defaults stringForKey:kApplicationVersion]; | |
NSData *passCodeData = [defaults dataForKey:kPasscodeSettings]; | |
if (passCodeData) { | |
_passCodeSettings = [NSKeyedUnarchiver unarchiveObjectWithData:passCodeData]; | |
} else { | |
_passCodeSettings = [PassCodeSettings new]; | |
} | |
} | |
return self; | |
} | |
+ (instancetype)defaultSettings { | |
static InAppSettings *instance = nil; | |
static dispatch_once_t onceToken = 0; | |
dispatch_once(&onceToken, ^{ | |
instance = [InAppSettings new]; | |
}); | |
return instance; | |
} | |
- (void)setApplicationVersion:(NSString *)applicationVersion { | |
[self willChangeValueForKey:@"applicationVersion"]; | |
_applicationVersion = applicationVersion; | |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | |
[defaults setObject:applicationVersion forKey:kApplicationVersion]; | |
[defaults synchronize]; | |
[self didChangeValueForKey:@"applicationVersion"]; | |
} | |
- (void)savePasscodeSettings { | |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | |
[defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:_passCodeSettings] forKey:kPasscodeSettings]; | |
[defaults synchronize]; | |
} | |
@end | |
@implementation PassCodeSettings | |
- (id)initWithCoder:(NSCoder *)aDecoder { | |
self = [super init]; | |
if (self) { | |
_attemptCount = [aDecoder decodeIntegerForKey:@"attemptCount"]; | |
_isPasscodeEnabled = [aDecoder decodeBoolForKey:@"isPassCodeEnabled"]; | |
_checkInterval = [aDecoder decodeDoubleForKey:@"checkInterval"]; | |
_nextCheckDate = [NSDate dateWithTimeIntervalSince1970:[aDecoder decodeDoubleForKey:@"nextCheckDate"]]; | |
} | |
return self; | |
} | |
- (void)encodeWithCoder:(NSCoder *)aCoder { | |
[aCoder encodeBool:_isPasscodeEnabled forKey:@"isPassCodeEnabled"]; | |
[aCoder encodeInteger:_attemptCount forKey:@"attemptCount"]; | |
[aCoder encodeDouble:_checkInterval forKey:@"checkInterval"]; | |
[aCoder encodeDouble:[_nextCheckDate timeIntervalSince1970] forKey:@"nextCheckDate"]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment