Reading and Writing Text Files in iPhone OS 3.0

Matthew Campbell, June 24th

iStock_000007449744XSmall.jpgReading and writing text files are one of the most basic things you need to do in iPhone development. In iPhone OS 3.0 things have changed a little bit here and there with some methods being deprecated.

I just wrote two example methods that you can use to see how you can turn a NSString into a text file and reverse the process.

Write the Contents of your NSString to the Filesystem

All I needed to do is create a NSString that has five lines in it and then I used the writeToFile method to save the contents. This version of writeToFile is like others that I have wrote about but it has a few more parameters.

Read Something from the Filesystem and Display It

Of course, once you write something to the filesystem you will want to use it again in the future. Here is a method I wrote as an example of this operation.

Discussion

That is it – you can do similar things with content like images and almost anything else with the NSData class. There is also a method in NSString (check out the header files in XCode) which advertises similar functionality for URL so it would look like you can do the same thing with your webserver. I had no luck with this. It was trivial to read a text file from my server, but I could not write files using the methods there; I know this can be done, just not with this very simple approaches.

Learn How To Make Your Own iPhone and iPad iOS apps!

If you are completely new to iPhone programming and want to start to developing iPhone apps this ebook will give you everything you need to get started today.

You Get the 5 Essential Things You Need to Make iPhone Apps

This ebook is much different than the other books out there: it really is a *system* that includes ONLY what you need to know to make iPhone applications. Not only that, but you will get some real insight on how to make a product and how to set up your code in the best way the first time.

Plus, you will get plenty of bonuses including exclusive source code projects. For more information, head on over to the website for the ebook:

Head over to How To Make An iPhone App eBook Website website to find out how to get Matt’s eBook and the bonus source code.

[...] in iPhone OS 3.0 June 24, 2009, 2:06 pm Filed under: Uncategorized (My Original Blog Post: http://howtomakeiphoneapps.com/2009/06/reading-and-writing-text-files-in-iphone-os-3-0/) Reading and writing text files are one of the most basic things you need to do in iPhone [...]

  • Hours of videos, demos & screen casts
  • SOURCE CODE
  • Detailed HANDS-ON Exercises
  • Much Much Much More
  • UIKit
  • Objective-C
  • Core Data
  • More!
  • Learn How To Make An iPhone App right now using my proven system.

    PS: Tons of us are having a blast living off our own code, join us now. Click here to get all the details.