We crate here a files in the application’s Home directory. To make things interesting, we load a web page from the Internet using the http protocol and store that html file in tmp. After that, we use a web view to load the html from the tmp directory and present it to user. We will see, these tasks can be easily achieved using the rich APIs available.
Step 1: Create a Window base application using the template . Give the name FileCreate.
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: Highlight the classes folder in Groups & Files. Select File -> New File from the menu. Select the UIViewController subclass from the CocoaTouch Classes and click next, give the name of the file FileCreateViewController.m and ensure the Also Create “FileCreateViewController.h”.
Step 4: The declaration of the application delegate class used in the file creation and local file view. So make the following changes in the FileCreateAppDelegate.h file.
@class FileCreateViewController;
@interface FileCreateAppDelegate : NSObject {
UIWindow *window;
FileCreateViewController *mainctrl;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
Step 5: The implementation of the application delegate. The delegate simply uses the FileCreateViewController as a subview of the main window.
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
mainctrl = [[FileCreateViewController alloc] initWithNibName:nil bundle:nil];
[window addSubview:mainctrl.view];
[window makeKeyAndVisible];
}
Step 6: In the ViewController header file we create UIWebView instance which will be used to visualize the contents of the local file in tmp. We declares two methods for the creation and visualization of the html file in tmp.
UIWebView *webView;
}
-(void) createAFileInTMP;
-(void) loadWebViewWithFileInTMP;
Step 7: In the FileCreateViewController.m file, the viewDidLoad method is invoked once the view has been loaded. It creates the file by invoking the createAFileInTMP method and after that it loads the web view with the downladed file by invoking the loadWebViewWithFileInTMP method.
#import <UIKit/UIKit.h>
#import "FileCreateViewController.h"
@implementation FileCreateViewController
- (void)loadView {
CGRect rectFrame = [UIScreen mainScreen].applicationFrame;
webView = [[UIWebView alloc] initWithFrame:rectFrame];
webView.scalesPageToFit = YES;
self.view = webView;
}
- (void)viewDidLoad {
[self createAFileInTMP];
[self loadWebViewWithFileInTMP];
}
-(void) loadWebViewWithFileInTMP
{
NSFileManager *fileManager =[NSFileManager defaultManager];
NSData *data;
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/file.html"];
data = [fileManager contentsAtPath:fileName];
[webView loadData:data MIMEType:@"text/html" textEncodingName:@"UTF-8"
baseURL :[NSURL URLWithString:@"http://www.rss-specifications.com"]];
}
-(void) createAFileInTMP
{
NSFileManager *fileManager =[NSFileManager defaultManager];
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/file.html"];
NSURL *theURL = [[NSURL alloc] initWithScheme:@"http"
host:@"www.rss-specifications.com"
path:@"/display-rss.htm"];
NSData *data = [[NSData alloc] initWithContentsOfURL:theURL];
BOOL filecreationSuccess = [fileManager createFileAtPath:fileName contents:data attributes:nil];
if(filecreationSuccess == NO){
NSLog(@"Failed to create the html file");
}
[theURL release];
[data release];
}
- (void)dealloc {
[webView release];
[super dealloc];
}
Step 8: Now compile your application and run onto the simulator.











