Skip to content

Instantly share code, notes, and snippets.

@gpinigin
Last active December 19, 2015 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gpinigin/5895129 to your computer and use it in GitHub Desktop.
Save gpinigin/5895129 to your computer and use it in GitHub Desktop.
#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
#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