I am using NSURLSession to download a large video file in background. In this iOS application I implemented pause, resume and stop downloading feature.
Here is my code :
in .h
@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
@property (nonatomic, strong) NSURLSession *session;
in .m
NSURLSessionConfiguration *sessionConfiguration;
float ver = [[[UIDevice currentDevice] systemVersion] floatValue];
if(ver >= 8)
{
sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"sessionID"];
}
else
{
sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"sessionID"];
}
sessionConfiguration.HTTPMaximumConnectionsPerHost = 1;
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate:self
delegateQueue:nil];
For stop downloading I am using this method:
- (void)stopDownloading:(id)sender {
// Cancel the task.
[self.fdi.downloadTask cancel];
}
It works quite well with a large amount of files, but there is an inconvenience. When I stop downloading then Memory which is used in downloading is not released so the application size is continuously for stopped downloading tasks.