iphone download text file
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)
Mac OS X (10.5.4)
self.fileManager = [NSFileManager defaultManager];
NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
self.documentsDirectory = [filePaths objectAtIndex: 0];
[self.fileManager changeCurrentDirectoryPath:self.documentsDirectory];
NSLog(@"The documents directory is: %@", self.documentsDirectory);
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];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
// 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"
}
+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error
+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error
iphone download text file