Skip to content

Instantly share code, notes, and snippets.

@ttran4040
Created July 7, 2014 00:28
Show Gist options
  • Save ttran4040/5173434c5fc267e4aac8 to your computer and use it in GitHub Desktop.
Save ttran4040/5173434c5fc267e4aac8 to your computer and use it in GitHub Desktop.
Objective C Serialization
// RMSerialization.m
#import "RMContext.h"
@implementation RMContext
+ (NSString*) serializationPath
{
NSFileManager* fileManager = [[NSFileManager alloc] init];
NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier];
NSArray* urlPaths = [fileManager URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
NSURL* appDirectory = [[urlPaths objectAtIndex:0] URLByAppendingPathComponent:bundleID isDirectory:YES];
if (![fileManager fileExistsAtPath:[appDirectory path]]) {
[fileManager createDirectoryAtURL:appDirectory withIntermediateDirectories:NO attributes:nil error:nil];
}
NSLog(@"Serialization Path = %@", [appDirectory path]);
return [appDirectory path];
}
+ (void) saveUser: (RMUser*) user
{
NSString * path = [NSString stringWithFormat:@"%@/user", [RMContext serializationPath]];
[NSKeyedArchiver archiveRootObject:user toFile:path];
NSLog(@"Saved User to Path: %@", path);
}
+ (BOOL) isUserLoggedIn
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString * path = [NSString stringWithFormat:@"%@/user", [RMContext serializationPath]];
NSLog(@"Checked Path: %@", path);
return ([fileManager fileExistsAtPath:path]);
}
+ (RMUser*) getCurrentUser
{
NSString * path = [NSString stringWithFormat:@"%@/user", [RMContext serializationPath]];
NSLog(@"Getting user from Path: %@", path);
return [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}
+ (void) signOut
{
// TODO: Implement this
}
@end
// RMSerialization.h
// Copyright (c) 2014 Laplacian. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "RMUser.h"
@interface RMContext : NSObject
+ (NSString*) serializationPath;
+ (void) saveUser: (RMUser*) user;
+ (BOOL) isUserLoggedIn;
+ (RMUser*) getCurrentUser;
+ (void) signOut;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment