Skip to content

Instantly share code, notes, and snippets.

@Kozlov-V
Last active June 24, 2016 09:09
Show Gist options
  • Save Kozlov-V/9d9605e4faf90c666166 to your computer and use it in GitHub Desktop.
Save Kozlov-V/9d9605e4faf90c666166 to your computer and use it in GitHub Desktop.
NSURLSessionUploadTask
2.3).NSURLSessionUploadTask
Upload Task sends data/file and it supports background uploads when the app is not running.
2.3.1) Create Upload task with system provided delegate methods.
/* Creates an upload task with the given request. The body of the request will be created from the file referenced by fileURL */
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL;
/* Creates an upload task with the given request. The body of the request is provided from the bodyData. */
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData;
/* Creates an upload task with the given request. The previously set body stream of the request (if any) is ignored and the URLSession:task:needNewBodyStream: delegate will be called when the body payload is required. */
- (NSURLSessionUploadTask *)uploadTaskWithStreamedRequest:(NSURLRequest *)request;
If you are using the above API, you need to implement below NSURLSessionTaskDelegate method.
Note: NSURLSessionUploadTask is not having any special delegate class. Delegate method is in NSURLSessionTaskDelegate
/* Sent periodically to notify the delegate of upload progress. This
* information is also available as properties of the task.
*/
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didSendBodyData:(int64_t)bytesSent
totalBytesSent:(int64_t)totalBytesSent
totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend;
2.3.2) Create Upload Task with custom delegate methods.
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment