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; } }
October 31st, 2011 at 11:10
i want to get the content of URL :
http://something.com/sth.mp3
i want to play it using audioPlayer .. and want to save it on the Database
how can i do that?
November 1st, 2011 at 19:10
@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.
November 13th, 2011 at 10:21
How can I upload the image from my device to the web server and get data about the image return to my device.
November 17th, 2011 at 03:44
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
-Chris
December 8th, 2011 at 19:39
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;
while(!feof(pFile))
{
fscanf (pFile, “%s %f %f”, loc,&x,&y);
count++;
}
December 8th, 2011 at 19:41
or is there a better way to import my data into my ViewDidLoad for my annotation?
My file is called the text.txt
December 13th, 2011 at 03:57
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
December 29th, 2011 at 00:57
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.
January 29th, 2012 at 19:17
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
February 16th, 2012 at 03:20
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 ) :
{“level”:”ok”,”inf”:”connected”}
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
April 9th, 2012 at 03:58
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….
-(IBAction)SaveNote{
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]);
}
}
April 14th, 2012 at 04:02
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?
Cheers
Andrew
May 26th, 2012 at 08:17
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]?
June 27th, 2013 at 04:43
Thanks alot…