Version 1.4 Sep 1. Thank you Freedy.
Version 1.3 Aug 6. Thank you Chris B.
Version 1.2 May 1. Thank you Pierre M.
Version 1.1 January 20
Copyright © 2009 Servin Corporation. http://servin.com
The iPhone OS, like Mac OS X, provides both Apple specific file I/O functions, and Unix-type file I/O functions.
For example, you can use Apple specific functions to find the paths related to your application. You can also use Apple specific Object-C classes to read/write files.
An alternative is to use Unix-specific functions to read/write files. In addition to the standard open/close/read/write, you can do memory mapping, where the file is mapped directly into your address space.
In this Servin Mini-Course, you will learn how to use Apple-specific functions and class methods to perform file I/O.
If Xcode is not already running, start it up:
At this point, you should see the Xcode menu at the top of your desktop.
With Xcode running, create a new Xcode project:
At this point, you should see Xcode open a new window that shows a number of files.
Go ahead and build the default application:
In this exercise, you will edit FileIoAppDelegate.h and FileIoAppDelegate.m to add a UITextView as a subview to the UIWindow.
FileIoAppDelegate.h FileIoAppDelegate.m
#import <UIKit/UIKit.h>
@interface FileIoAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UITextView *textView;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UITextView *textView;
@end
#import <FileIoAppDelegate.h>
@implementation FileIoAppDelegate
@synthesize window;
@synthesize textView;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create instance of UITextView
self.textView = [[UITextView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// Add text
self.textView.text = @"Output will go here...";
// Make non-editable
self.textView.editable = NO;
// Add as subview to window
[window addSubview:self.textView];
// Decrement our usage count
[self.textView release];
[window makeKeyAndVisible];
}
As a review, in this exercise you added code to the FileIoAppDelegate class to create a UITextView, setting it so that it cannot be edited. You also added text to the UITextView, and then added the UITextView to the subview of the window.
In this exercise, you will create a file named MyFile.txt as a sample text file that you will later read and display in the iPhone window.
- Info.plist - MainWindow.xib
- MainWindow.xib - Info.plist - MyFile.txt
This is a text file. The file will be displayed on the iPhone. That is all for now.
As a review, you created a text file named MyFile.txt in the Resources directory of your project.
In this exercise, you will use the NSHomeDirectory() function to retrieve the path to the home directory of your application:
NSString *NSHomeDirectory(void);
An important point: the location of your home directory is different depending on whether you are running on the iPhone Simulator or on an actual device (iPhone or iPod Touch).
- FileIoAppDelegate.h - FileIoAppDelegate.m
#import <FileIoAppDelegate.h>
@implementation FileIoAppDelegate
@synthesize window;
@synthesize textView;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create instance of UITextView
self.textView = [[UITextView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]];
NSString *homeDir = NSHomeDirectory();
// Add text
self.textView.text = homeDir;
// Make non-editable
self.textView.editable = NO;
// Add as subview to window
[window addSubview:self.textView];
// Decrement our usage count
[self.textView release];
[window makeKeyAndVisible];
}
/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/... - OR (on an actual device) - /var/mobile/Applications/...
As a review, in this exercise you used the NSHomeDirectory() function to find the pathname of your applications home directory.
In this exercise, you will use the NSTemporaryDirectory() function to retrieve the path to the temporary directory for use by your application.
NSString *NSTemporaryDirectory(void);
An important point: the location of your temporary directory is different depending on whether you are running on the iPhone Simulator or on an actual device (iPhone or iPod Touch).
#import <FileIoAppDelegate.h>
@implementation FileIoAppDelegate
@synthesize window;
@synthesize textView;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create instance of UITextView
self.textView = [[UITextView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// Get home directory
NSString *homeDir = NSHomeDirectory();
// Get temporary directory
NSString *tempDir = NSTemporaryDirectory();
// Format output
NSString *s =
[NSString stringWithFormat:@"homeDir:\n"
@"%@\n"
@"tempDir:\n"
@"%@\n",
homeDir,
tempDir];
// Add text
self.textView.text = s;
// Make non-editable
self.textView.editable = NO;
// Add as subview to window
[window addSubview:self.textView];
// Decrement our usage count
[self.textView release];
[window makeKeyAndVisible];
}
homeDir: /Users/student/Library/Application Support/iPhone/Simulator/User/Applications/... tempDir: /var/folders/7m/.... - OR (on an actual device) /private/var/mobile/Applications/.../tmp
As a review, in this exercise you used the NSTemporaryDirectory() function.
In this exercise, you will use the NSSearchPathForDirectoriesInDomains() function to retrieve various paths:
NSArray * NSSearchPathForDirectoriesInDomains( NSSearchPathDirectory directory, //NSDocumentDirectory or NSCachesDirectory NSSearchpathDomainMask domainMask, //NSUserDomainMask BOOL exppandTilde); // YES
An important point: Although a NSArray object is returned, you will only use the first array entry (index 0), which will contain an NSString.
#import <FileIoAppDelegate.h>
@implementation FileIoAppDelegate
@synthesize window;
@synthesize textView;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create instance of UITextView
self.textView = [[UITextView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// Get home directory
NSString *homeDir = NSHomeDirectory();
// Get temporary directory
NSString *tempDir = NSTemporaryDirectory();
// Get documents directory
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *docDir = [arrayPaths objectAtIndex:0];
// Format output
NSString *s =
[NSString stringWithFormat:@"homeDir:\n"
@"%@\n"
@"tempDir:\n"
@"%@\n"
@"docDir:\n"
@"%@\n",
homeDir,
tempDir,
docDir];
// Add text
self.textView.text = s;
// Make non-editable
self.textView.editable = NO;
// Add as subview to window
[window addSubview:self.textView];
// Decrement our usage count
[self.textView release];
[window makeKeyAndVisible];
}
homeDir: /Users/student/Library/Application Support/iPhone/Simulator/User/Applications/... tempDir: /var/folder/7m/.... docDir: /Users/student/Library/Application Support/.../Documents
As a review, in this exercise you used the NSSearchPathForDirectoriesInDomain() function.
In this exercise, you will use the NSBundle class to report the path to the applications resources. The applications resources are stored in the application bundle, and the path to this bundle can be found with the method pathForRecoure:ofType:.
#import <FileIoAppDelegate.h>
@implementation FileIoAppDelegate
@synthesize window;
@synthesize textView;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create instance of UITextView
self.textView = [[UITextView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// Get home directory
NSString *homeDir = NSHomeDirectory();
// Get temporary directory
NSString *tempDir = NSTemporaryDirectory();
// Get documents directory
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *docDir = [arrayPaths objectAtIndex:0];
NSString *myFilePath = [[NSBundle mainBundle]
pathForResource:@"MyFile"
ofType:@"txt"];
// Format output
NSString *s =
[NSString stringWithFormat:@"homeDir:\n"
@"%@\n"
@"tempDir:\n"
@"%@\n"
@"docDir:\n"
@"%@\n"
@"myFilePath:\n"
@"%@\n",
homeDir,
tempDir,
docDir,
myFilePath];
// Add text
self.textView.text = s;
// Make non-editable
self.textView.editable = NO;
// Add as subview to window
[window addSubview:self.textView];
// Decrement our usage count
[self.textView release];
[window makeKeyAndVisible];
}
homeDir: /Users/student/Library/Application Support/iPhone/Simulator/User/Applications/... tempDir: /var/folder/7m/.... docDir: /Users/student/Library/Application Support.../Documents myFilePath: /Users/student/Library/Application Support/.../FileIo.App/MyFile.txt
As a review, in this exercise you used the NSBundle class to determine the path to the MyFile.txt, which is stored as part of the resource bundle.
In this exercise, you will use the NSString class to read the MyText.txt file.
#import <FileIoAppDelegate.h>
@implementation FileIoAppDelegate
@synthesize window;
@synthesize textView;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create instance of UITextView
self.textView = [[UITextView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// Get home directory
NSString *homeDir = NSHomeDirectory();
// Get temporary directory
NSString *tempDir = NSTemporaryDirectory();
// Get documents directory
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString docDir = [arrayPaths objectAtIndex:0];
NSString *myFilePath = [[NSBundle mainBundle]
pathForResource:@"MyFile"
ofType:@"txt"];
NSString *myFileContents = [NSString stringWithContentsOfFile:myFilePath
encoding:NSUTF8StringEncoding
error:nil];
// Format output
NSString *s =
[NSString stringWithFormat:@"homeDir:\n"
@"%@\n"
@"tempDir:\n"
@"%@\n"
@"docDir:\n"
@"%@\n"
@"myFilePath:\n"
@"%@\n"
@"Contents of file:\n"
@"%@\n",
homeDir,
tempDir,
docDir,
myFilePath,
myFileContents];
// Add text
self.textView.text = s;
// Make non-editable
self.textView.editable = NO;
// Add as subview to window
[window addSubview:self.textView];
// Decrement our usage count
[self.textView release];
[window makeKeyAndVisible];
}
homeDir: /Users/student/Library/Application Support/iPhone/Simulator/User/Applications/... tempDir: /var/folder/7m/.... docDir: /Users/student/Library/Application Support/.../Documents myFilePath: /Users/student/Library/Application Support/.../FileIo.App/MyFile.txt Contents of file: This is a text file. This file will be displayed on the iPhone. That is all for now.
As a review, in this exercise you used the NSString class to read the contents of MyFile.txt.
In this exercise, you will use the NSString class to write a new file named NewText.txt.
#import <FileIoAppDelegate.h>
@implementation FileIoAppDelegate
@synthesize window;
@synthesize textView;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create instance of UITextView
self.textView = [[UITextView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// Get home directory
NSString *homeDir = NSHomeDirectory();
// Get temporary directory
NSString *tempDir = NSTemporaryDirectory();
// Get documents directory
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString docDir = [arrayPaths objectAtIndex:0];
NSString *myFilePath = [[NSBundle mainBundle]
pathForResource:@"MyFile"
ofType:@"txt"];
NSString *myFileContents = [NSString stringWithContentsOfFile:myFilePath
encoding:NSUTF8StringEncoding
error:nil];
// Format output
NSString *s =
[NSString stringWithFormat:@"homeDir:\n"
@"%@\n"
@"tempDir:\n"
@"%@\n"
@"docDir:\n"
@"%@\n"
@"myFilePath:\n"
@"%@\n"
@"Contents of file:\n"
@"%@\n",
homeDir,
tempDir,
docDir,
myFilePath,
myFileContents];
// Create pathname to Documents directory
NSString *newFilePath = [docDir stringByAppendingString:@"/NewFile.txt"];
// Write string to file
[s writeToFile:newFilePath
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];
// Add text
self.textView.text = s;
// Make non-editable
self.textView.editable = NO;
// Add as subview to window
[window addSubview:self.textView];
// Decrement our usage count
[self.textView release];
[window makeKeyAndVisible];
}
$ cat /Users/student/Library/Application Support/.../Documents/NewFile.txt ...
As a review, in this exercise you used the NSString class to write the contents of a NSString MyFile.txt.
Feel free to contact the author for any of the following:
Updated 2009 Sep 1
Content viewable on all web browsers, including smart mobile phone devices.
Copyright © 1995-2009 Servin Corporation. All rights reserved.