Question

I am currently developing an iPad iOS 6 Application which uses async downloads. To receive progress information i used the delegate NSURLConnectionDownloadDelegate. The download and the progress received by

– connection:didWriteData:totalBytesWritten:expectedTotalBytes:

works just fine.

However once the download is finished, i do not know how to extract the data out of the NSURL destinationURL provided by the delegatemethod

– connectionDidFinishDownloading:destinationURL:

The destinationURL received in String looks like "/private/var/mobile/Applications/7CB3B194-9E79-4F0B-ACFD-7B87AA8C7BAF/tmp/filename.mp4"

Then i try to extract the data from the given NSURL:

NSData *data = [[NSData alloc] initWithContentsOfURL:destinationURL];

The data however is empty...

NSLog(@"data Size: %@", data.length);     //prints out 0

Has someone the same issue by using NSURLConnectionDownloadDelegate? Any ideas how to solve?


Edit: I tried to copy the file first, and then use [NSData initWithContentsOFFile] as suggested, but the data size is still 0.

- (void) connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL
{

[[NSFileManager defaultManager] copyItemAtPath:destinationURL.path toPath:DEST_PATH error:nil];

NSData *data = [[NSData alloc] initWithContentsOfFile:DEST_PATH];

NSLog(@"data Size: %i", data.length); //still returns 0..
}

Edit2: I can extract the NSData when using sendAsynchronousRequest method of NSURLConnection. However like this, i am not able to determine the progress of the download...

[NSURLConnection sendAsynchronousRequest:theRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if (data){
NSLog(@"Size of the data: %i Bytes(s)", data.length); //works fine
[data writeToFile:DEST_PATH atomically:YES];
//From this point i can use the file at DEST_PATH

NSLog(@"Succeeded!");
}
else if (error)
NSLog(@"%@",error);
}];
Was it helpful?

Solution

Check if the file exists. I have the same problem and I have to use NSURLConnectionDataDelegate and save the data manually and that's why: There is an error with NSURLConnectionDownloadDelegate related with some file extensions.

Also you could see answers to the same question here: NSURLConnectionDownloadDelegate file issue

OTHER TIPS

/private/var/mobile/Applications/7CB3B194-9E79-4F0B-ACFD-7B87AA8C7BAF/tmp/filename.mp4

is a path, not a URL. Try initWithContentsOfFile: instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top