I am developing an app to upload multiple files using NSURLSession, right now my files are successfully uploaded. But now what i want to achieve is to pause,resume and cancel the uploads just like we do in download tasks. Is it possible.? Any help would be appreciated. Thnx
I have studied alot but could find nothing .After i tried this on my code assuming this as a download task ,I came to know that we can "pause , resume" upload tasks as well using NSURLSession just we do in downloading tasks.
To pause a task simply call [yourUploadTask suspend];
to resume [yourUploadTask resume];
To cancel [yourUploadTask cancel];
It might help someone working on uploadTask using NSURLSession.
You can try to use the suspend(), resume() methods inherited from URLSessionTask, but resume() will cause the upload to fail. You can work around this in your upload class by automatically retrying upload errors (i.e. any non-permanent error other than 400s), but that means restarting every pending upload again from the beginning and losing the data that's already been uploaded.
There isn't a cancel(byProducingResumeData completionHandler: @escaping (Data?) -> Void) for URLSessionUploadTask like there is for URLSessionDownloadTask, so there is no state data saved to restore and continue the upload. To implement one would require rolling our own, which I haven't done (yet), but I believe requires specific server support to implement, and the user of lower level networking APIs.
来源:https://stackoverflow.com/questions/27813799/pause-resume-cancel-upload-task-using-nsurlsession-uploadtask