How to Save Data Using File Path in iPhone

Introduction

In this I will create a single view application. Here I use a text field from outlet to take input from the keyboard and one button "save", to save data statically on a particular path.

file-path-in-iPhone.png
For new entry we edit on @"datafile.dat".

Step 1

Open Xcode by double-clicking on it.

Step 2

Create a New XCode Project by clicking on it.

Step 3

Now select Single View Application and click on Next.

Step 4

Now provide your Product Name and Company Identifier then click on Next.

Step 5

Select the location where you want to save your project and click on Create.

Step 6

Here we write code.

AppDelegate.h

#import <UIKit/UIKit.h>
@class
file_exampleViewController;
@interface
file_exampleAppDelegate : UIResponder <UIApplicationDelegate>
@property
(strong, nonatomic) UIWindow *window;
@property
(strong, nonatomic) file_exampleViewController *viewController;
@end

AppDelegate.m

#import
"file_exampleViewController.h"
@implementation
file_exampleAppDelegate
- (
void)dealloc
{
[
_window release];

[
_viewController release];
[
super dealloc];
}

- (
BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


self
.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self
.viewController = [[[file_exampleViewController alloc] initWithNibName:@"file_exampleViewController" bundle:nil] autorelease];

self
.window.rootViewController = self.viewController;
[
self.window makeKeyAndVisible];

return
YES;
}

@end


ViewController.h

#import <UIKit/UIKit.h>
@interface
file_exampleViewController : UIViewController
@property
(strong, nonatomic) IBOutlet UITextField *textBox;

- (
IBAction)saveText:(id)sender;
@end

ViewController.m

#import "file_exampleViewController.h"

@interface file_exampleViewController ()

@end

@implementation file_exampleViewController

@synthesize textBox;

- (void)saveText:(id)sender

{
NSFileManager
*filemgr;

NSData
*databuffer;

NSString
*dataFile;

NSString
*docsDir;

NSArray
*dirPaths;
filemgr = [
NSFileManager defaultManager];
dirPaths =
NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths
objectAtIndex:0];
dataFile = [docsDir

stringByAppendingPathComponent
: @"datafile.dat"];
databuffer = [
textBox.text

dataUsingEncoding
: NSASCIIStringEncoding];
[filemgr
createFileAtPath: dataFile

contents
: databuffer attributes:nil];
}

- (
void)didReceiveMemoryWarning
{
[
super didReceiveMemoryWarning];
}

- (
void)viewDidLoad
{
[
super viewDidLoad];

NSFileManager
*filemgr;

NSString
*dataFile;

NSString
*docsDir;

NSArray
*dirPaths;
filemgr = [
NSFileManager defaultManager];

// Identify the documents directory
dirPaths =
NSSearchPathForDirectoriesInDomains(

NSDocumentDirectory
, NSUserDomainMask, YES);
docsDir = [dirPaths
objectAtIndex:0];

// Build the path to the data file
dataFile = [docsDir
stringByAppendingPathComponent:

@"datafile.dat"
];

// Check if the file already exists

if
([filemgr fileExistsAtPath: dataFile])
{

// Read file contents and display in textBox

NSData
*databuffer;
databuffer = [filemgr
contentsAtPath: dataFile];

NSString
*datastring = [[NSString alloc]

initWithData
: databuffer
encoding:
NSASCIIStringEncoding];

textBox
.text = datastring;
}
}
- (
void)viewDidUnload
{
[
super viewDidUnload];

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

self
.textBox = nil;
}
@end

ViewController.Xib

ViewControlle.xib-in-iPhone.png

Step 7

Now select the iPhone platform to see output in the Simulator.

Output:

Output1 in iPhone:

Output1-in-IPhone.png

Output2 in iPhone:

Output2-in-IPhone.png

To quit the app we right-click on the simulator and select quit.

quit-app-in-iPhone.png

To run this app again we click on run.

run-app-in-iPhone.png

Output3 in iPhone:

Output3-in-IPhone.png

Now after lunching this app we will see "Hello" already written in text field.

So we can say here hello is saved permanently for the specified directory path. To remove it we edit in code.  


Similar Articles