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);
}];