NSObject+LocalStorage
// NSObject+LocalStorage.h
// Created by Avneesh.minocha on 19/05/15.
#import
typedef NS_ENUM(NSUInteger, kModalType) {
kTypeLogin = 1,
};
@interface NSObject (LocalStorage)
/**
* This method is use to save any modal in local storage
*
* @param type name of modal in a form of enum.
*/
- (void)saveLocally:(kModalType)type;
/**
* This method is use to fetch any modal from local storage
*
* @param type name of modal in a form of enum.
*
* @return modal object
*/
- (instancetype)fetchLocally:(kModalType)type;
/**
* This method is use to delete any modal from local storage
*
* @param type name of modal in a form of enum.
*/
-(void)removeAllinLocal:(kModalType)type;
@end
//NSObject+FFLocalStorage.m
// Created by Avneesh.minocha on 19/05/15.
#import "NSObject+LocalStorage.h"
@implementation NSObject (LocalStorage)
-(NSString *)keybykModalType:(kModalType)type
{
NSString *key = @"";
switch (type) {
case kModalTypeLogin:
key = @"kTypeLogin";
break;
default:
break;
}
return key;
}
-(void)removeAllinLocal:(kModalType)type
{
[self removeallData:type];
}
- (void)saveLocally:(kModalType)type
{
NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:self];
[[NSUserDefaults standardUserDefaults] setObject:archivedData forKey:[self keybykModalType:type]];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (instancetype)fetchLocally:(kModalType)type
{
NSData *archivedData = [[NSUserDefaults standardUserDefaults] objectForKey:[self keybykModalType:type]];
return [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
}
-(void)removeallData:(kModalType)type
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:[self keybykModalType:type]];
}
//How to use
//call on any model object to save the object value in persistent memory
[loginResponse saveLocally:kModalTypeLogin];
Comments
Post a Comment