Skip to content

Commit

Permalink
Ive said it before but I finally think the model shit is sorted out, …
Browse files Browse the repository at this point in the history
…with Model class securely archiving now
  • Loading branch information
Tim Sawtell committed May 23, 2014
1 parent 2b7a200 commit 57d7674
Show file tree
Hide file tree
Showing 12 changed files with 1,480 additions and 1,405 deletions.
6 changes: 6 additions & 0 deletions App/Controller/Application/Commands/CommandCenter.h
Expand Up @@ -17,4 +17,10 @@

@interface CommandCenter : NSObject

+ (NSData *)securelyArchiveRootObject:(id)object withKey:(NSString *)key;

+ (id)securelyUnarchiveData:(NSData *)data
withClass:(Class)class
withKey:(NSString *)key;

@end
39 changes: 39 additions & 0 deletions App/Controller/Application/Commands/CommandCenter.m
Expand Up @@ -17,4 +17,43 @@

@implementation CommandCenter

+ (NSData *)securelyArchiveRootObject:(id)object withKey:(NSString *)key
{
//Use secure encoding because files could be transfered from anywhere by anyone
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

//Ensure that secure encoding is used
[archiver setRequiresSecureCoding:YES];
@try {
[archiver encodeObject:object forKey:key];
} @catch (NSException *e) {
NSLog(@"%@", e);
} @finally {
[archiver finishEncoding];
}

return data;
}

+ (id)securelyUnarchiveData:(NSData *)data
withClass:(Class)class
withKey:(NSString *)key
{
id returnObject = nil;
if (nil == data) return nil;
@try {
//Use secure encoding because files could be transfered from anywhere by anyone
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
//Ensure that secure encoding is used
[unarchiver setRequiresSecureCoding:YES];
returnObject = [unarchiver decodeObjectOfClass:class forKey:key];
}
@catch (NSException *exception) {
NSLog(@"%@ failed to unarchive PicketList: %@", NSStringFromSelector(_cmd), exception);
}

return returnObject;
}

@end
5 changes: 5 additions & 0 deletions App/Model/CoreDataModel/MogenTemplate/human.m.motemplate
Expand Up @@ -19,6 +19,11 @@
// Implement custom saving logic here
}

+ (BOOL)supportsSecureCoding
{
return YES;
}

#pragma mark Abstract method overrides

<$foreach Relationship noninheritedRelationshipsInIDKeyPathTopologicalOrder do$><$if ! Relationship.isTransient$><$if Relationship.userInfo.destinationEntityIDKeyPath $>
Expand Down
6 changes: 4 additions & 2 deletions App/Model/Model.h
Expand Up @@ -14,11 +14,13 @@
*/

#import <Foundation/Foundation.h>
#import "Book.h"

@interface Model : NSObject
@interface Model : NSObject <NSSecureCoding>

@property (nonatomic, strong) Book *book;

+ (Model*) sharedModel;
+ (BOOL)supportsSecureCoding;
- (void)save;

@end
20 changes: 18 additions & 2 deletions App/Model/Model.m
Expand Up @@ -17,6 +17,7 @@
#import "TestModel.h"
#import "NSFileManager+DirectoryLocations.h"
// Edit Scheme, Run setting, Arguments tab, environment variables. Preprocessor macros do not work when the main target is built before the test target.
NSString * const kModelKey = @"model";

@implementation Model

Expand All @@ -30,7 +31,8 @@ + (NSString*)savedDataPath

+ (Model*)savedModel
{
Model *model = [NSKeyedUnarchiver unarchiveObjectWithFile: [Model savedDataPath]];
NSData *savedData = [NSData dataWithContentsOfFile:[Model savedDataPath]];
Model *model = [CommandCenter securelyUnarchiveData:savedData withClass:[Model class] withKey:kModelKey];
if (!model) {
return [Model new];
}
Expand All @@ -57,19 +59,27 @@ - (void)save
@try {
@synchronized(self)
{
[NSKeyedArchiver archiveRootObject:self toFile:[Model savedDataPath]];
NSData *data = [CommandCenter securelyArchiveRootObject:self withKey:kModelKey];
if (![data writeToFile:[Model savedDataPath] atomically:YES]) {
[NSException raise:@"Model did not save" format:@"The data model did not save"];
}
}
} @catch (NSException *exception) {
DLog(@"Exeption in save method: %@", exception);
}
}


#pragma mark - initializing

- (id) initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.book = [aDecoder decodeObjectOfClass:[Book class] forKey:@"book"];
// if you have a collection of model objects as a property on this class, you must decode like this
// assume you have an array of books
// self.books = [aDecoder decodeObjectOfClasses:[NSSet setWithObjects:[NSArray class], [Book class], nil] forKey:@"books"];
}
return self;
}
Expand All @@ -79,4 +89,10 @@ - (void) encodeWithCoder:(NSCoder *)aCoder
[aCoder encodeObject:self.book forKey:@"book"];
}

+ (BOOL)supportsSecureCoding
{
return YES;
}


@end
2 changes: 1 addition & 1 deletion App/Model/ModelObject.h
Expand Up @@ -52,7 +52,7 @@
+ (NSSet *)dictionaryRepresentationKeys;

- (void)awakeFromDictionaryRepresentationInit;
+ (BOOL)supportsSecureCoding;

@end


Expand Down
10 changes: 5 additions & 5 deletions App/Model/ModelObject.m
Expand Up @@ -38,6 +38,11 @@ - (void) encodeWithCoder: (NSCoder*) aCoder
// If we add ivars/properties, here's where we'll save them
}

+ (BOOL)supportsSecureCoding
{
return YES;
}

+ (id)modelObjectWithClass:(Class)aClass FromObject:(ModelObject *)object
{
if (object == nil || ![aClass isSubclassOfClass:[ModelObject class]]) {
Expand Down Expand Up @@ -98,11 +103,6 @@ - (id)copyWithZone:(NSZone *)zone

@synthesize sourceDictionaryRepresentation;

+ (BOOL)supportsSecureCoding
{
return YES;
}

@end


Expand Down
5 changes: 5 additions & 0 deletions App/Model/ModelObjects/Human/Book.m
Expand Up @@ -19,6 +19,11 @@ - (void) encodeWithCoder: (NSCoder*) aCoder
// Implement custom saving logic here
}

+ (BOOL)supportsSecureCoding
{
return YES;
}

#pragma mark Abstract method overrides


Expand Down
2 changes: 2 additions & 0 deletions App/Supporting Files/App-Prefix.pch
Expand Up @@ -18,9 +18,11 @@

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "ModelObjectHeaders.h"
#import "TSNetworking.h"
#import "Constants.h"
#import "Appearances.h"
#import "CommandCenter.h"
#import "Model.h"
#import "NSStringHelper.h"
#import "TSCommandRunner.h"
Expand Down
2 changes: 1 addition & 1 deletion Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 57d7674

Please sign in to comment.