As u have used NSUserDefaults to store values of type whatever you want to choose from the List of Valid Object to set for a specified Key.
Like if you want to set NSDictionary Object to NSUserDefaults then you use code like this [[NSUserDefaults standardUserDefaults] setObject:dictionaryObject forKey:@"myDictionary"];
Similarly Storage File will work for you.
You Just need to use this file like
[[SharedManager sharedManager] setObject:dictionaryObject forKey:@"myDictionary"];
How it works--
It has one static instance of storage file that it returns when you call [SharedManager sharedManager].
As it is static instance, Any change to this file will reflect in all the class in which you are using it.And change from anywhere to this class will update it to be used in another class.
It has its own plist file that keep your value persist when application closed.And return it to you whenever you call it again after launching application next time.
Plus point is that you can also store CGRect and CGSize to this file.
SharedManager.h File
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
// SharedManager.h
// Demo
//
// Created by Prince Kumar Sharma on 02/08/13.
// Copyright (c) 2013 Prince Kumar Sharma. All rights reserved.
#import <Foundation/Foundation.h>
@interface SharedManager : NSObject
{
NSFileManager *fileManager;
NSMutableDictionary *mySharedObject;
}
+ (id)sharedManager;
//Setting Objects
-(void)setObject:(id)object ForKey:(NSString*)key;
-(void)setBool:(BOOL)boolval ForKey:(NSString*)key;
-(void)setInteger:(int)integerVal ForKey:(NSString*)key;
-(void)setFloat:(float)floatVal Forkey:(NSString*)key;
-(void)setDouble:(double)doubleVal ForKey:(NSString*)key;
-(void)setURL:(NSURL*)urlVal ForKey:(NSString*)key;
-(void)setData:(NSData*)dataVal ForKey:(NSString*)key;
-(void)setRect:(CGRect)rectVal ForKey:(NSString*)key;
-(void)setSize:(CGSize)sizeVal ForKey:(NSString*)key;
//Getting Objects
-(id)ObjectForKey:(NSString*)key;
-(BOOL)boolForKey:(NSString*)key;
-(int)integerForKey:(NSString*)key;
-(float)floatForKey:(NSString*)key;
-(double)doubleForKey:(NSString*)key;
-(NSURL*)urlForKey:(NSString*)key;
-(NSData*)dataForKey:(NSString*)key;
-(CGRect)rectForKey:(NSString*)key;
-(CGSize)sizeForKey:(NSString*)key;
@end
SharedManager.m File
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 "SharedManager.h"
NSString *filePath;
@implementation SharedManager
#pragma mark Creating Shared Instance.
+ (id)sharedManager {
static SharedManager *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
- (id)init {
if (self = [super init]) {
fileManager=[NSFileManager defaultManager];
filePath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/sharedManager.plist"];
if ([fileManager fileExistsAtPath:filePath]) {
mySharedObject=[[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
}else{
mySharedObject=[[NSMutableDictionary alloc] init];
}
}
return self;
}
-(void)setObject:(id)object ForKey:(NSString*)key
{
[mySharedObject setObject:object forKey:key];
[mySharedObject writeToFile:filePath atomically:YES];
}
-(void)setBool:(BOOL)boolval ForKey:(NSString*)key
{
[mySharedObject setValue:[NSString stringWithFormat:@"%@",[NSNumber numberWithBool:boolval]] forKey:key];
[mySharedObject writeToFile:filePath atomically:YES];
}
-(void)setInteger:(int)integerVal ForKey:(NSString*)key
{
[mySharedObject setValue:[NSString stringWithFormat:@"%i",integerVal] forKey:key];
[mySharedObject writeToFile:filePath atomically:YES];
}
-(void)setFloat:(float)floatVal Forkey:(NSString*)key
{
[mySharedObject setValue:[NSString stringWithFormat:@"%f",floatVal] forKey:key];
[mySharedObject writeToFile:filePath atomically:YES];
}
-(void)setDouble:(double)doubleVal ForKey:(NSString*)key
{
[mySharedObject setValue:[NSString stringWithFormat:@"%f",doubleVal] forKey:key];
[mySharedObject writeToFile:filePath atomically:YES];
}
-(void)setURL:(NSURL*)urlVal ForKey:(NSString*)key
{
[mySharedObject setValue:[urlVal absoluteString] forKey:key];
[mySharedObject writeToFile:filePath atomically:YES];
}
-(void)setData:(NSData*)dataVal ForKey:(NSString*)key
{
[mySharedObject setValue:[NSString stringWithUTF8String:[dataVal bytes]] forKey:key];
[mySharedObject writeToFile:filePath atomically:YES];
}
-(void)setRect:(CGRect)rectVal ForKey:(NSString*)key
{
NSMutableArray *mArray=[[NSMutableArray alloc] init];
[mArray addObject:[NSString stringWithFormat:@"%f",rectVal.origin.x]];
[mArray addObject:[NSString stringWithFormat:@"%f",rectVal.origin.y]];
[mArray addObject:[NSString stringWithFormat:@"%f",rectVal.size.width]];
[mArray addObject:[NSString stringWithFormat:@"%f",rectVal.size.height]];
[mySharedObject setObject:mArray forKey:key];
[mySharedObject writeToFile:filePath atomically:YES];
}
-(void)setSize:(CGSize)sizeVal ForKey:(NSString*)key
{
NSMutableArray *mArray=[[NSMutableArray alloc] init];
[mArray addObject:[NSString stringWithFormat:@"%f",sizeVal.width]];
[mArray addObject:[NSString stringWithFormat:@"%f",sizeVal.height]];
[mySharedObject setObject:mArray forKey:key];
[mySharedObject writeToFile:filePath atomically:YES];
}
//Getting Objects
-(id)ObjectForKey:(NSString*)key
{
return [mySharedObject objectForKey:key];
}
-(BOOL)boolForKey:(NSString*)key
{
return [[mySharedObject objectForKey:key] boolValue];
}
-(int)integerForKey:(NSString*)key
{
return [[mySharedObject objectForKey:key] intValue];
}
-(float)floatForKey:(NSString*)key
{
return [[mySharedObject objectForKey:key] floatValue];
}
-(double)doubleForKey:(NSString*)key
{
return [[mySharedObject objectForKey:key] doubleValue];
}
-(NSURL*)urlForKey:(NSString*)key
{
return [[mySharedObject objectForKey:key] absoluteURL];
}
-(NSData*)dataForKey:(NSString*)key
{
return [[mySharedObject objectForKey:key] dataUsingEncoding:NSUTF8StringEncoding];
}
-(CGRect)rectForKey:(NSString*)key
{
NSArray *rectArray=[[NSArray alloc] initWithArray:[mySharedObject objectForKey:key]];
CGRect rect;
if (rectArray.count!=0) {
rect.origin.x=[[rectArray objectAtIndex:0] floatValue];
rect.origin.y=[[rectArray objectAtIndex:1] floatValue];
rect.size.width=[[rectArray objectAtIndex:2] floatValue];
rect.size.height=[[rectArray objectAtIndex:3] floatValue];
}
return rect;
}
-(CGSize)sizeForKey:(NSString*)key
{
NSArray *sizeArray=[[NSArray alloc] initWithArray:[mySharedObject objectForKey:key]];
CGSize size;
if (sizeArray.count!=0) {
size.width=[[sizeArray objectAtIndex:0] floatValue];
size.height=[[sizeArray objectAtIndex:1] floatValue];
}
return size;
}
@end
---------------------
I have used this file as you can see below how to set data to it.
SharedManager *sm=[SharedManager sharedManager];
NSArray *myArray=[[NSArray alloc] initWithObjects:@"Rohan",@"Folks",@"Alien",@"Animals",@"Karizma",nil];
NSDictionary *myDictionary=[[NSDictionary alloc] initWithObjectsAndKeys:@"Banana",@"Fruit",@"Zebra",@"Animal",@"Parrot",@"Birds",nil];
NSString *myString=@"I am String";
NSData *data=[myString dataUsingEncoding:NSUTF8StringEncoding];
CGRect rect=CGRectMake(97, 443, 200, 450);
CGSize size=CGSizeMake(340, 564);
[sm setSize:size ForKey:@"mysize"];
[sm setRect:rect ForKey:@"myrect"];
[sm setData:data ForKey:@"mydata"];
[sm setBool:YES ForKey:@"mybool"];
[sm setObject:myString ForKey:@"myStr"];
[sm setObject:myArray ForKey:@"myarray"];
[sm setObject:myDictionary ForKey:@"mydict"];
And here is reference code to use this for getting info's you have set.
SharedManager *sm=[SharedManager sharedManager];
NSLog(@"my size string is %@",NSStringFromCGSize([sm sizeForKey:@"mysize"]));
NSLog(@"my rectangle is %@",NSStringFromCGRect([sm rectForKey:@"myrect"]));
NSLog(@"my data to string is %@", [[NSString alloc] initWithData:[sm dataForKey:@"mydata"] encoding:NSUTF8StringEncoding]);
NSLog(@"my bool is %i",[sm boolForKey:@"mybool"]);
NSLog(@"my array is %@",[sm ObjectForKey:@"myarray"]);
NSLog(@"my dict is %@",[sm ObjectForKey:@"mydict"]);
NSLog(@"my string is %@",[sm ObjectForKey:@"myStr"]);