iphone download text file

hi,
i need to download txt files for my app, but i'm totally clueless on how to get about doing it.
does anyone have any sample code or snippets that i can refer to?
thanks greatly!

Mac OS X (10.5.4)

Posted on Oct 6, 2008 8:49 AM

Reply
9 replies

Oct 6, 2008 11:28 PM in response to Luciola

This is one way to access the downloaded files:


self.fileManager = [NSFileManager defaultManager];
NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
self.documentsDirectory = [filePaths objectAtIndex: 0];
[self.fileManager changeCurrentDirectoryPath:self.documentsDirectory];


Standard procedure for iPhone apps is to put working documents in the "Documents" folder, as above. Setting up the file manager in this way in the app's init code then lets you use the relative path (i.e., just the file name) for all other file operations involving NSFileManager.

If you're running your app in the Simulator, the directory on your HD will change and be randomly generated for each rebuild of your program. The best way to find it is just to output it to the log after this initialization code.


NSLog(@"The documents directory is: %@", self.documentsDirectory);


This will output the full path of the application's current working directory on your hard drive.

Oct 7, 2008 5:19 AM in response to Luciola

think i got it! for everyone else that might need it:

first, get path:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths objectAtIndex:0 ;


to write:

NSString *appFile = documentsDirectory stringByAppendingPathComponent:fileName;
data writeToFile:appFile atomically:YES;

to read:

NSString *appFile = documentsDirectory stringByAppendingPathComponent:fileName;
NSData *myData = NSData alloc initWithContentsOfFile:appFile autorelease;

and thanks to everyone that's left a response here!
p.s. i left out all the square brackets cos it messed up the msg.

Message was edited by: Luciola

Oct 6, 2008 1:25 PM in response to Luciola

When you say "download" do you mean that they are on a server somewhere - that is you can't include them in your bundle at build time?

If that's the case, you need to use NSHTTPRequest - and you need an HTTP server running on your server.


NSURL* completeURL = [[NSURL alloc]initWithString:@"http://www.mydomain.com/full/path/of/file.txt"];

NSMutableURLRequest* request =[NSMutableURLRequest requestWithURL:completeURL];
[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];


Your data will be delivered by the delegate's

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

Note that this method will be called multiple times as the data arrives. I suggest that you read up on NSURLRequest. If your data is very small and you don't mind the program potentially blocking whilst it downloads, there are synchronous utility functions

Oct 6, 2008 10:48 AM in response to Luciola

First, you must include the text file in XCode in the Resource folder; than you will have access to that file in the "bundle", as in the following example:

// get the Resource path name
NSString *dir = [[NSBundle mainBundle] resourcePath];
// get an array with all .txt files in the bundle
NSArray *files = [NSBundle pathsForResourcesOfType:@"txt" inDirectory:dir];
for (NSString *fileName in files) // loop over array
{
// do whatever you want with the file named "filename"
}

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

iphone download text file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.