Thursday, September 27, 2012

Saving value when minimize/closing application



//Store value
-viewDidLoad{
UIApplication *myApp = [UIApplication sharedApplication];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:myApp];
}

- (void) applicationWillTerminate:(UIApplication *)application{

NSString *str1 = @"saved";
NSArray *saveValues = [[NSArray alloc] initWithObjects: str1, nil];
[saveValues writeToFile:[self saveFilePath] atomically:YES];
[saveValues release];

}

- (NSString *) saveFilePath{

NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:@"save.plist"];

}

//Retreive value

NSString *myPath = [self saveFilePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];
if(fileExists){
NSArray *saveValueArray = [[NSArray alloc] initWithContentsOfFile:myPath];
NSString *str = [saveValueArray objectAtIndex:0];
NSLog(@"Found- %@ ", str);
}else{
NSLog(@"Not Found !!!");
}

No comments:

Post a Comment