How to use Archiving in iPhone

In this example we will see Archiving using pList . This is the very easy to use. Let see how it will be worked.

In this example we will see Archiving using pList . This is the very easy to use. Let see how it will be worked.

Step 1: Create a View base application using template. Give the application name “usingPlist”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Xpand classes and notice Interface Builder created the usingPlistViewController class for you. Expand Resources and notice the template generated a separate nib, usingPlistViewController.xib, for the “usingPlist”.

Step 4: We need to add one more class in the Project. Select Classes -> add -> NewFile ->Cocoa Touch Class -> Objective C class -> Subclass of -> select NSObject and give the class name “fourLines”.

Step 5: We add two pList in the Resource folder and give names “newDocumentData.plist” and “data.plist”.

Step 6: In the usingPlistViewController.h file we have added UITextField and UINavigationController and two method.So make the following changes in the file.

#import <UIKit/UIKit.h>
#import “TextViewController.h”#define kFileName @”newDocumentData”
#define fDataKey @”Data”

@interface usingPlistViewController : UIViewController<UITextViewDelegate> {

IBOutlet UITextField *text1;
IBOutlet UITextField *text2;
IBOutlet UITextField *text3;
IBOutlet UITextField *text4;
UINavigationController *navController;

@property(nonatomic,retain) IBOutlet UITextField *text1;
@property(nonatomic,retain) IBOutlet UITextField *text2;
@property(nonatomic,retain) IBOutlet UITextField *text3;
@property(nonatomic,retain) IBOutlet UITextField *text4;
@property(nonatomic,retain) IBOutlet UITextView *textView;

-(NSString *) dataFilePath;
-(void) handleNotification : (NSNotification *)notify;
– (IBAction) updateText:(id) sender;

Step 7: Double click the usingPlistViewController.xib file and open it to the Interface  Builder. Open the view window, first drag four UITextField and UILabel and place it to the view window, change the label name into Line 1: ,Line 2: ,Line 3: , Line 4:(see the figure below). Now Connect File’s Owner icon to the View icon and select view. Select File’s Owner icon to the first textfield and select text1. Follow the same procedure for next three textfield and select text2, text3 and text4 . Select the first textfield and bring up Connection Inspector, and drag Did End On Exit to the File’s Owner icon and select updateText: Action. Follow the same procedure for next three textfileds. You have done all the connection now save the usingPlistViewController.xib, close it and go back to the Xcode.

Step 8: Open the usingPlistViewController.m file and make the following changes in the file:

-(NSString*)dataFilePath{        NSString *filePath = [[NSBundle mainBundle] pathForResource:@”newDocumentData” ofType:@”plist”];
NSString *fileContent =[ [NSString alloc] initWithContentsOfFile:filePath];
NSLog(@”file content : %@”,fileContent);
return filePath;

-(void)handleNotification :(NSNotification *)notify{


fourLines *fourlines = [[fourLines alloc] init];

fourlines.field1 = text1.text;

fourlines.field2 = text2.text;
fourlines.field3 = text3.text;
fourlines.field4 = text4.text;
fourlines.textViewField = textView.text;

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

NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
[archiver encodeObject:fourlines forKey:fDataKey];
[archiver finishEncoding];

[data writeToFile:[self dataFilePath] atomically:YES];
[fourlines release];
[archiver release];
[data release];


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
– (void)viewDidLoad {
[super viewDidLoad];

UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@”pushNotify” object:app];
//      [self.textView setDelegate:self];

NSString * filePath = [self dataFilePath];

NSData *data = [[NSMutableData  alloc]initWithContentsOfFile:filePath];

NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]

fourLines *fourlines = [unarchiver decodeObjectForKey:fDataKey];
[unarchiver finishDecoding];

text1.text = fourlines.field1;
text2.text = fourlines.field2;
text3.text = fourlines.field3;
text4.text = fourlines.field4;
textView.text = fourlines.textViewField;

[unarchiver release];
[data release];


– (void)textViewDidBeginEditing:(UITextView *)textView

NSLog(@”self.view : %@”,self.view);
navController = [[UINavigationController alloc] initWithRootViewController:self];
navController.navigationBar.frame = CGRectMake(0, 0, 320,40);
[self.view addSubview:navController.navigationBar];

NSLog(@”editing begin”);
// provide my own Save button to dismiss the keyboard
UIBarButtonItem* saveItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:@selector(saveAction:)];
self.navigationItem.rightBarButtonItem = saveItem;
[saveItem release];

– (void)saveAction:(id)sender
// finish typing text/dismiss the keyboard by removing it as the first responder
[self.textView resignFirstResponder];
self.navigationItem.rightBarButtonItem = nil;   // this will remove the “save” button
[navController.navigationBar removeFromSuperview];

Step 9: In the fourLines.h file make the following changes :

#import <Foundation/Foundation.h>#define kField1Key @”Field1″
#define kField2Key @”Field2″
#define kField3Key @”Field3″
#define kField4Key @”Field4″
#define kField5Key @”Field5″

@interface fourLines : NSObject <NSCoding,NSCopying>{

NSString *field1;
NSString *field2;
NSString *field3;
NSString *field4;
NSString *textViewField;

@property(nonatomic,retain)NSString *field1;
@property(nonatomic,retain)NSString *field2;
@property(nonatomic,retain)NSString *field3;
@property(nonatomic,retain)NSString *field4;
@property(nonatomic,retain)NSString *textViewField;

Step 10: Open the fourLines.m file and make the following changes:

self = [super init];
return self;
}#pragma mark NSCoding

-(void)encodeWithCoder:(NSCoder *)encoder{

NSLog(@”encode :%@”,field1);
[encoder encodeObject:field1 forKey:kField1Key];
[encoder encodeObject:field2 forKey:kField2Key];
[encoder encodeObject:field3 forKey:kField3Key];
[encoder encodeObject:field4 forKey:kField4Key];
[encoder encodeObject:textViewField forKey:kField5Key];

-(id) initWithCoder:(NSCoder *)decoder{

if(self = [super init]){
NSLog(@”decode :%@”,field1);
self.field1 = [decoder decodeObjectForKey:kField1Key];
self.field2 = [decoder decodeObjectForKey:kField2Key];
self.field3 = [decoder decodeObjectForKey:kField3Key];
self.field4 = [decoder decodeObjectForKey:kField4Key];
self.textViewField = [decoder decodeObjectForKey:kField5Key];
return self;
#pragma mark NSCopying
-(id)copyWithZone:(NSZone *)zone{

fourLines *copy= [[[self class] allocWithZone:zone] init];

field1 = [self.field1 copy];
field2 = [self.field2 copy];
field3 = [self.field3 copy];
field4 = [self.field4 copy];
textViewField = [self.textViewField copy];
NSLog(@”copy :%@”,copy.field1);
return copy;


Step 11: Now compile and run the application in the Simulator.

You can Download SourceCode from here usingArchiveExercise