Search Posts in my Blog

Tuesday 24 April 2012

Create, Read and Delete a file in your iPhone App




I recently had to add some functionality to the app I’m working on in which an email would be sent by the app with an attachment that contained data collected by the app. Well I already had the email portion working but wasn’t sure how I wanted to do the attachment. It needed to be an html document and my first thought was to create an html file, attach it to the email and then delete the file. As it turned out I decided not to create the file and just created the attachment on the fly. But since I had already written a small app to test creating, reading and deleting a file I figured I would go ahead and turn it into a tutorial. Because honestly when I was first figuring out how to do the file stuff I couldn’t find a very simple tutorial on the subject. So here it is.
finished look

1.) Create a new View-based application and name it FileTutorial.
2.) The first thing we are going to do is set up our view. So double click FileTutorialViewController.xib to open it up in File Builder.
3.) We’re going to add three buttons and a UIWebView. If you need help doing this refer back to the Interface Builder Tutorial. After you add these user elements it should/could look something like this.
create the view
4.) Save your work in Interface Builder and go back to Xcode. Open up FileTutorialViewController.h. We are going to add an NSFileManager, an NSArray and 2 NSStrings. We also need to add an IBOutlet for the UIWebView we added in Interface Builder. We’ll set the properties for all of these and define three actions that will be called from our buttons.
the header file
5.) Next we open up FileTutorialViewController.m. and synthesize our variables we defined in the header file.
synthesize statement
6.) Now we need to implement our variables, we’ll do this in the viewDidLoad method

createFileAction method
7.) We need to implement our three IBActions that will get called when a user selects one of the buttons. First we’ll do createFileAction. This method creates a very basic html file using an NSMutableString, then it checks to see if the file already exists and if it doesn’t it creates a file at the defined path and then writes our string to the file.

createFileAction method
8.) The second method is deleteFileAction. This method simply checks to see if the file exists and if it does it removes it from the path.
deleteFileAct</p>
<p>9.) The third method is readFile. This is the method that actually reads the file contents. First it checks to see if the file exists, if it does it reads the contents of the file and puts them into a NSString, then it puts that string into our webView that will be displayed to the user.</p>
<p><img src=
10.) Before any of this will work we need to go back into Interface Builder and connect out IBActions and the UIWebView. Connect each action to appropriate button and the webView to the UIWebView.
create the view
11.) You can now save everything, go into Xcode, click Build and Run and test it out. If you make the console visible you can follow along with what is happening.

createFileAction method
create the view
That’s it, pretty simple stuff. I didn’t explain things in much detail because the naming of the variable and methods really identified what was happening pretty well.
iHope this helped you.
You Can DOWNLOAD complete CODE HERE.

No comments:

Post a Comment