Resume upload task using NSURLSessionUploadTask after app is killed?

一世执手 提交于 2019-12-11 00:15:14

问题


I am using the following code to upload image using NSURLSessionUploadTask

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"Success: %@ %@", response, responseObject);
    }
}];
[uploadTask resume];

My file gets uploaded on server properly, but if the app is killed in between the upload is in progress then i am unable to restore the state of my upload by getting session identifier and then after resume it.

After googling out I found that the below delegate method will be called (after the relaunch of my app) if my upload is interrupted, but this method doesn't seems to be called in my case.

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

Am I using the correct method to identify session identifier or there is some other method to implement my requirement?

来源:https://stackoverflow.com/questions/34523048/resume-upload-task-using-nsurlsessionuploadtask-after-app-is-killed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!