Hello,
Today i download pdf files in background and save it like this :
Code:
-(void)savePdfFile:(NSString *)object
{
NSAutoreleasePool * pool=[[NSAutoreleasePool alloc] init];
NSString * theFileName=[NSString stringWithFormat:@"%@.pdf",[[object lastPathComponent] stringByDeletingPathExtension]];
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory2 = [paths2 objectAtIndex:0];
NSString *pdfPath2 = [documentsDirectory2 stringByAppendingPathComponent:theFileName];
//test if file exists
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pdfPath2];
if (!fileExists)
{
DebugLog(@"file n'existe pas");
NSURL *urlFile = [NSURL URLWithString:object];
NSData *data = [NSData dataWithContentsOfURL:urlFile];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:theFileName];
[data writeToFile:pdfPath atomically:YES];
}
[pool release];
}
Some people said that it is not very. They say that's i need to use NSURLConnection. But my method is not good ?
How do i do ??
Thank you in advance.