Example of downloading a file and saving it in a directory in Documents
In the .h file
@interface AppMain : NSObject
//-ADD THIS DELEGATE
{
FileDownloader *fileDownloader1; //-ADD THIS
In the .m file
//********** DOWNLOAD FILE **********
- (void) SomeMethodName
{
NSLog(@"STARTING DOWNLOAD");
if (!fileDownloader1)
fileDownloader1 = [[FileDownloader alloc] init];
[fileDownloader1 setDelegate:self];
[fileDownloader1 DownloadFile:@"http://www.somedomain.com/somefile.html"];
}
//********** FILE DOWNLOAD FINISHED CALLBACK **********
- (void)FileDownloadFinished:(NSData *)file
{
NSString *filePath;
NSError *error;
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"OurFiles"];
//Create a new cache directory
if (![[NSFileManager defaultManager] createDirectoryAtPath:path
withIntermediateDirectories:NO
attributes:nil
error:&error])
{
NSLog(@"Create directory error: %@", error);
}
//Save the file
filePath = [path stringByAppendingPathComponent:@"somefile.html"];
[[NSFileManager defaultManager] createFileAtPath:filePath
contents:file
attributes:nil];
}