Accessing bytes from NSURLSessionDownloadTask as they are downloaded
In my application, I am currently using NSURLSessionDownloadTask to fetch a file over HTTP.
This class provides a useful delegate interface for tracking its progress and retrieving bytes after the download is complete via NSURLSessionDownloadDelegate , however I have not been able to find any way to access the bytes as they are downloaded (before the download is complete).
Is it possible to access these bytes or do I need to download the file using some other mechanism?
source to share
@ C6Silver's answer is a method in NSURLSessionDataDelegate , not NSURLSessionDownloadDelegate . Here's the way you need to implement:
- (void)URLSession:(NSURLSession *)session
downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;
You can use a parameter totalBytesWritten
to access the data you want.
Edit . I didn't understand this question.
You will have to use NSURLSessionDataTask and not DownloadTask and then use the delegate method in NSURLSessionDataDelegate :
- (void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data;
This will allow you to access the data as you receive it.
source to share