Apr 07

First you need to add your file to the Resources folder of your Xcode project. Then you can access the file like this (assuming the file is called MyFile.txt):

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {
	// do something useful

Here’s a complete example reading a help text file into a UIWebView.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HelpDoc" ofType:@"htm"];
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
if (htmlData) {
	[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://iphoneincubator.com"]];

If you want to read the file into a string, which you can then display in a UITextView, for example, then do this:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"important" ofType:@"txt"];
if (filePath) {
	NSString *myText = [NSString stringWithContentsOfFile:filePath];
	if (myText) {
		textView.text= myText;

written by Nick \\ tags: , , ,

87 Responses to “How To Read a File From Your Application Bundle”

  1. mb Says:

    i want to get the content of URL :

    i want to play it using audioPlayer .. and want to save it on the Database
    how can i do that?

  2. Nick Says:

    @mb: If you just want to play audio from a URL you can use [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; If you want to save the data locally use [NSData dataWithContentsOfURL:url]; to get the audio data, and then save it to a file with writeToFile. The method for saving to a database depends on the specifics of the database. But if you have an NSData object it shouldn’t bee too difficult.

  3. Grapong Says:

    How can I upload the image from my device to the web server and get data about the image return to my device.

  4. Chris Says:

    Good Afternoon ,

    I have written script to download a file and store it in the documents folder , now how can i access it ?

    Thanks and Regards

  5. Jeff Says:

    I would like to import an address data for annotation and I have the following code, what is wrong with this code?

    Please do help me. I am trying on making a first app.

    THank you very much in advance.

    FILE * pFile;
    char scoreFile[512];

    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths lastObject];
    NSString *fileName = [documentDirectory stringByAppendingPathComponent:@”text.txt”];

    pFile = fopen( fileName,”r”);

    NSString loc=NULL;
    char *loc = (char *) calloc(2000, sizeof(char));
    float x=0;
    float y=0;
    int count=1;

    fscanf (pFile, “%s %f %f”, loc,&x,&y);



  6. Jeff Says:

    or is there a better way to import my data into my ViewDidLoad for my annotation?

    My file is called the text.txt

  7. calvin Says:

    Hi, Nick

    i want to do an application for getting the image from photolibrary,
    (i.e) just when a user taps (choose file) button, photo library was to be shown, after that when a user select any image, then the path of that image should be displayed in the text field above the (choose file) button

  8. Deepti Says:

    I think we have to add this – encoding:NSASCIIStringEncoding error:nil

    NSString *myText=[NSString stringWithContentsOfFile:filePath encoding:NSASCIIStringEncoding error:nil];

    So that we will get our text.

  9. Poorva Says:

    how do i take an input from a user in the text field??
    and then If the user types into the text entry field, your application code must:

    1.find the element in the question array that best matches what the user entered

    2.display the matched element in the question label

    thanks in advance

  10. Max Says:

    Trying to read the header of an html file, I have an NSString like this
    NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    and my json file is (in an html one ) :
    and I would like to know if its possible to convert this json file (the file returned by the server ) into an html text to get info from the header

    thanks in advance

  11. ramesh Says:

    i m working on textview..i m entering sum text in text view and i wil save the text with btn prewss event..it wil save on local path…bt how to create a duplicate file with the same name like text2.txt….

    NSError *error;

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@”Note.txt”];
    NSString *savedString = textView.text;

    NSLog(@”The Save file is:%@”, savedString);
    NSLog(@” file exists at path:%@”, documentTXTPath);

    [[savedString description] writeToFile:documentTXTPath atomically:YES encoding:NSUTF8StringEncoding error:&error];

    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentTXTPath error:NULL];
    int Count;
    for (Count = 0; Count < (int)[directoryContent count]; Count++)
    NSLog(@"File %d: %@", (Count + 1), [directoryContent objectAtIndex:Count]);


  12. Andrew Says:

    Nice article. I had a similar piece of code from a tutorial that loaded the contents of a video file for uploading to a webpage:

    NSData *videoData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@”Movie” ofType:@”mov”]];

    In my app the video file is in the documents directory. I have tried a few different ways to modify this line to take the documents directory path and filename but have not succeeded because I suspect the dataWithContentOfFile isn’t getting the correct type of information.

    I have the path to the video file in an NSString, which I have read in from a file.

    I would like videoData to be loaded with the video file contents from the documents directory, in exactly the same way as the code above achieves.

    Can you help?



  13. Jeff B. Says:

    Background: I’m an experienced programmer, but a noob when it comes to iPhone/XCode.

    I’m running into a strange problem. I’m loading a TXT file from the Resource bundle as described above, and the contents are used to load a Picker. Works great in the Simulator and on all of the iTouches I’ve tested. However, when I run it on the iPhone, picContents is nil and I receive my error message.

    Here’s the code:

    NSString *picPath = [[NSBundle mainBundle] pathForResource:@”Indiana Rules of Trial Procedure Index Jan-12″ ofType:@”txt”];
    NSString *picContents = [[NSString alloc] initWithContentsOfFile:picPath encoding:NSUTF8StringEncoding error:NULL];

    if( picContents == nil ) {
    MsgBoxOK(@”Error loading picker data from Resource Bundle.”, picPath);

    P.S. – Also, why can’t I [picPath Release]?

  14. Anand Gautam Says:

    Thanks alot…

Leave a Reply