Вот что я делаю, я установил счетчик для определения типа файла для сохранения:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
[connection release];
// Do something with the dataForConnection.
downloadCount ++;
// Copy Image Data to ur location using FileManager
//UIImage *img = [UIImage imageWithData:fileData];
NSString *saveDir;
if (downloadCount ==1)
saveDir = [NSString stringWithFormat:@"001.jpg"];
if (downloadCount == 2)
saveDir = [NSString stringWithFormat:@"001.mp3"];
//NSLog(@"saveDir=%@",saveDir);
NSArray * sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString * filePath = [[NSString alloc] initWithFormat: @"%@/%@", [sysPaths objectAtIndex: 0], saveDir];
NSLog(@"filePath=%@",filePath);
if([dataForConnection writeToFile:filePath atomically:YES])
NSLog(@"DONE COPYING");
else
NSLog(@"Write to device error");
//[fileData setLength:0];
if (downloadCount == 2)
{
downloadCount = 0;
play the MP3 and display the image file;
}
}
даже я запускаю приложение на симуляторе, файл был загружен без сообщения об ошибке, но иногда файл не открывается, даже я пытался открыть его из папки, мне говорили, что файл поврежден, я не понимаю что случилось, надеюсь, ты понимаешь, о чем я говорю.