//archivingData.m

-(id) initWithCoder:(NSCoder *)aDecoder

{

if (self = [super init]) {


self.email = [aDecoder decodeObjectForKey:@"email"];

self.pwd =[aDecoder decodeObjectForKey:@"pwd"];

}

return self;

}


-(void) encodeWithCoder:(NSCoder *)aCoder

{

[aCoder encodeObject:email forKey:@"email"];

[aCoder encodeObject:pwd forKey:@"pwd"];

}


//viewController.m

- (void)viewDidLoad {

NSString *path = [self DataPath];

if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {

NSMutableData *data = [[NSMutableData alloc] initWithContentsOfFile:path];

NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

archivingData *archiv = [unArchiver decodeObjectForKey:@"data"];

[unArchiver finishDecoding];

txtEmail.text = archiv.email;

txtPwd.text = archiv.pwd;

[data release];

[unArchiver release];

}

UIApplication *sharedApp = [UIApplication sharedApplication];

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

    [super viewDidLoad];

}



-(void)applicationWillTerminate:(NSNotification *)notif

{

archivingData *archiv = [[archivingData alloc] init];

archiv.email = txtEmail.text;

archiv.pwd = txtPwd.text;

NSMutableData *data = [[NSMutableData alloc] init];

NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

[archiver encodeObject:archiv forKey:@"data"];

[archiver finishEncoding];

[data writeToFile:[self DataPath] atomically:YES];

[archiv release];

[data release];

[archiver release];

}


-(NSString *)DataPath

{

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *directory = [path objectAtIndex:0];

return [directory stringByAppendingFormat:@"archivingData"];

}

//Document 디렉토리안에 저장되는게 아니라 DocumentsarchivingData 로 저장됨

'Object-C' 카테고리의 다른 글

SQLITE  (0) 2010.10.08
CoreData  (0) 2010.10.07
archiving  (0) 2010.10.07
pickerView  (0) 2010.10.07
imagePicker  (0) 2010.10.07
pageControl  (0) 2010.10.07

+ Recent posts