How to get backgroundSession NSURLSessionUploadTask response

[亡魂溺海] 提交于 2019-12-02 19:41:42

The URLSession:task:didCompleteWithError: method of NSURLSessionTaskDelegate should be called when your upload is done. Refer to the task.response object, which should be the NSHTTPURLResponse object.


I'm sure you're doing this, but the standard background upload task components are:

  1. Make a background session:

    NSURLSessionConfiguration *backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.domain.app"];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:backgroundConfiguration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    
  2. Use the NSURLSession method uploadTaskWithRequest:fromFile: method:

    NSURLSessionTask *task = [session uploadTaskWithRequest:request fromFile:fileURL];
    [task resume];
    

    With a background session, you must:

    • Use NSURLSessionUploadTask;

    • Use file-based rendition (you cannot use NSData based version);

    • Use delegate-based rendition cannot use data tasks; (b) NSData rendition of the NSURLSessionUploadTask; nor (c) a completion block rendition of the NSURLSessionUploadTask.

  3. With upload tasks, make sure to not call setHTTPBody of a NSMutableRequest. With upload tasks, the body of the request cannot be in the request itself.

  4. Make sure you implement the appropriate NSURLSessionDelegate, NSURLSessionTaskDelegate methods.

  5. Make sure to implement application:handleEventsForBackgroundURLSession: in your app delegate (so you can capture the completionHandler, which you'll call in URLSessionDidFinishEventsForBackgroundURLSession).

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