NSURLSession: how to start downloading again, but not resume to download?

534 views Asked by At

I use NSURLSession, and NSURLSessionDownloadTask to download o file. During the downloading time, I kill the application (by tapping on home button, and swipe off the application). I received an error "Cancel" in the following function as I expected:


@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {

....

self.downloadTask = nil;

...

}

the userinfo of the error contains the resume data, in order that later we can continue with downloading. However, I don't want that. What I want is when I re-open the application, it starts downloading again, but not resume to download.

I assign the self.downloadTask = nil, but it does not resolve the problem.

The following function I used to start downloading:


-(void) download:(NSURLRequest*) request {

    if (self.downloadTask != nil) return;
    self.session = [self sharedBackgroundSession];
    self.downloadTask = [self.session downloadTaskWithRequest:request];
    [self.downloadTask resume];
}

- (NSURLSession *)sharedBackgroundSession {

    static NSURLSession *session = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"abs.com.DownloadApp"];
        session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    });
    return session;
}

Note: I am using XCode6, Objective-C

0

There are 0 answers