Inherits from NSObject
Declared in NetworkManager.h
NetworkManager.m

Overview

Network manager

This creates a NSURLSession and manages an collection of NetworkTaskOperation objects. The key feature of this class is that you can use it to create NSOperation-based NSURLSessionTask objects, which are their own delegates (generally the session is, inexplicably, the delegate for NSURLSessionTaskDelegate and NSURLSessionTaskDelegate. But this session manager will maintain a collection of NetworkTaskOperation objects, and when it receives task-related delegate calls, it simply passes the call to the appropriate NetworkTaskOperation object.

In short, this, in conjunction with NetworkTaskOperation (and its subclasses), achieves task-based delegate calls.

Usage

  1. Create property to hold NetworkManager:

    @property (nonatomic, strong) NetworkManager *networkManager;
    
  2. Instantiate NetworkManager:

    self.networkManager = [[NetworkManager alloc] init];
    
  3. Create task and add it to the manager’s queue:

    NSOperation *operation = [self.networkManager downloadOperationWithURL:url didWriteDataHandler:^(NetworkDownloadTaskOperation *operation, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
         // now update the UI here
    } didFinishDownloadingHandler:^(NetworkDownloadTaskOperation *operation, NSURL *location, NSError *error) {
        // download is done
    }];
    
    [self.networkManager addOperation:operation];
    

Note: The progress/completion blocks will, by default, be called on the main queue. If you want to use a different GCD queue, specify a non-nil completionQueue value.

Tasks

Properties

  •   completionHandler

    The completion handler to be set by app delegate’s handleEventsForBackgroundURLSession and to be called by URLSessionDidFinishEventsHandler.

    property
  •   urlSessionDidFinishEventsHandler

    The block that will be called by URLSessionDidFinishEventsForBackgroundURLSession:.

    property
  •   didReceiveChallenge

    The block that will be called by URLSession:didReceiveChallenge:completionHandler:.

    property
  •   didBecomeInvalidWithError

    The block that will be called by URLSession:didBecomeInvalidWithError:.

    property
  •   didFinishDownloadingToURL

    The block that will be called by URLSession:downloadTask:didFinishDownloadingToURL:. Generally we keep the task methods at the task operation class level, but for background downloads, we may lose the operations when the app is killed.

    property
  •   didCompleteWithError

    The block that will be called by URLSession:task:didCompleteWithError:. Generally we keep the task methods at the task operation class level, but for background downloads, we may lose the operations when the app is killed.

    property
  •   credential

    Credential to be tried if receive session-level authentication challenge.

    property
  •   completionQueue

    The GCD queue to which completion/progress blocks will be dispatched. If nil, it will use dispatch_get_main_queue().

    property

Initialization methods

NetworkTaskOperation factory methods

NSOperationQueue utility methods

Properties

completionHandler

The completion handler to be set by app delegate’s handleEventsForBackgroundURLSession and to be called by URLSessionDidFinishEventsHandler.

@property (nonatomic, copy) void ( ^ ) ( void ) completionHandler

Declared In

NetworkManager.h

completionQueue

The GCD queue to which completion/progress blocks will be dispatched. If nil, it will use dispatch_get_main_queue().

@property (nonatomic, strong) dispatch_queue_t completionQueue

Discussion

Often, its useful to have the completion blocks run on the main queue (as you’re generally updating the UI). But if you’re doing something on a background thread that doesn’t rely on UI updates (or if performing tests in the absence of a UI), you might want to use a background queue.

Declared In

NetworkManager.h

credential

Credential to be tried if receive session-level authentication challenge.

@property (nonatomic, strong) NSURLCredential *credential

Declared In

NetworkManager.h

didBecomeInvalidWithError

The block that will be called by URLSession:didBecomeInvalidWithError:.

@property (nonatomic, copy) DidBecomeInvalidWithError didBecomeInvalidWithError

Discussion

This uses the following typedef:

typedef void(^DidBecomeInvalidWithError)(NetworkManager *manager,
                                         NSError *error);

Declared In

NetworkManager.h

didCompleteWithError

The block that will be called by URLSession:task:didCompleteWithError:. Generally we keep the task methods at the task operation class level, but for background downloads, we may lose the operations when the app is killed.

@property (nonatomic, copy) DidCompleteWithError didCompleteWithError

Discussion

This uses the following typedef:

typedef void(^DidCompleteWithError)(NetworkManager *manager,
                                    NSURLSessionTask *task,
                                    NSError *error);

Declared In

NetworkManager.h

didFinishDownloadingToURL

The block that will be called by URLSession:downloadTask:didFinishDownloadingToURL:. Generally we keep the task methods at the task operation class level, but for background downloads, we may lose the operations when the app is killed.

@property (nonatomic, copy) DidFinishDownloadingToURL didFinishDownloadingToURL

Discussion

This uses the following typedef:

typedef void(^DidFinishDownloadingToURL)(NetworkManager *manager,
                                         NSURLSessionDownloadTask *downloadTask,
                                         NSURL *location);

Declared In

NetworkManager.h

didReceiveChallenge

The block that will be called by URLSession:didReceiveChallenge:completionHandler:.

@property (nonatomic, copy) DidReceiveChallenge didReceiveChallenge

Discussion

This uses the following typedef:

typedef void(^DidReceiveChallenge)(NetworkManager *manager,
                                   NSURLAuthenticationChallenge *challenge,

Declared In

NetworkManager.h

urlSessionDidFinishEventsHandler

The block that will be called by URLSessionDidFinishEventsForBackgroundURLSession:.

@property (nonatomic, copy) URLSessionDidFinishEventsHandler urlSessionDidFinishEventsHandler

Discussion

This uses the following typedef:

typedef BOOL(^URLSessionDidFinishEventsHandler)(NetworkManager *manager);

Note: If this block calls the completion handler, it should return NO, to inform the default URLSessionDidFinishEvents method that it does not need to call the completionHandler. It should also make sure to nil the completionHandler after it calls it.

If this block does not call the completion handler itself, it should return YES to inform the default routine that it should call the completionHandler and perform the necessary clean-up.

Declared In

NetworkManager.h

Class Methods

backgroundSessionWithIdentifier:

Retrieve (and, if necessary create) background session manager

+ (instancetype)backgroundSessionWithIdentifier:(NSString *)identifier

Parameters

identifier

Background session identifier

Return Value

A session manager.

Declared In

NetworkManager.h

Instance Methods

addOperation:

Add operation.

- (void)addOperation:(NSOperation *)operation

Parameters

operation

The operation to be added to the queue.

Discussion

A convenience method to add operation to the network manager’s networkQueue operation queue.

Declared In

NetworkManager.h

dataOperationWithRequest:progressHandler:completionHandler:

Create data task operation.

- (NetworkDataTaskOperation *)dataOperationWithRequest:(NSURLRequest *)request progressHandler:(ProgressHandler)progressHandler completionHandler:(DidCompleteWithDataErrorHandler)didCompleteWithDataErrorHandler

Parameters

request

The NSURLRequest.

progressHandler

The method that will be called with as the data is being downloaded.

didCompleteWithDataErrorHandler

The block that will be called when the upload is done.

Return Value

Returns NetworkDataTaskOperation.

Discussion

Note: If you supply progressHandler, it is assumed that you will take responsibility for handling the individual data chunks as they come in. If you don’t provide this block, this class will aggregate all of the individual NSData objects into one final one for you.

Note: The progress/completion blocks will, by default, be called on the main queue. If you want to use a different GCD queue, specify a non-nil completionQueue value.

Declared In

NetworkManager.h

dataOperationWithURL:progressHandler:completionHandler:

Create data task operation.

- (NetworkDataTaskOperation *)dataOperationWithURL:(NSURL *)url progressHandler:(ProgressHandler)progressHandler completionHandler:(DidCompleteWithDataErrorHandler)didCompleteWithDataErrorHandler

Parameters

url

The NSURL.

progressHandler

The method that will be called with as the data is being downloaded.

didCompleteWithDataErrorHandler

The block that will be called when the upload is done.

Return Value

Returns NetworkDataTaskOperation.

Discussion

Note: If you supply progressHandler, it is assumed that you will take responsibility for handling the individual data chunks as they come in. If you don’t provide this block, this class will aggregate all of the individual NSData objects into one final one for you.

Note: The progress/completion blocks will, by default, be called on the main queue. If you want to use a different GCD queue, specify a non-nil completionQueue value.

Declared In

NetworkManager.h

downloadOperationWithRequest:didWriteDataHandler:didFinishDownloadingHandler:

Create download task operation.

- (NetworkDownloadTaskOperation *)downloadOperationWithRequest:(NSURLRequest *)request didWriteDataHandler:(DidWriteDataHandler)didWriteDataHandler didFinishDownloadingHandler:(DidFinishDownloadingHandler)didFinishDownloadingHandler

Parameters

request

The NSURLRequest.

didWriteDataHandler

The method that will be called with as the data is being downloaded.

didFinishDownloadingHandler

The block that will be called when the upload is done.

Return Value

Returns NetworkDownloadTaskOperation.

Discussion

Note: The progress/completion blocks will, by default, be called on the main queue. If you want to use a different GCD queue, specify a non-nil completionQueue value.

Declared In

NetworkManager.h

downloadOperationWithResumeData:didWriteDataHandler:didFinishDownloadingHandler:

Create download task operation.

- (NetworkDownloadTaskOperation *)downloadOperationWithResumeData:(NSData *)resumeData didWriteDataHandler:(DidWriteDataHandler)didWriteDataHandler didFinishDownloadingHandler:(DidFinishDownloadingHandler)didFinishDownloadingHandler

Parameters

resumeData

The NSData from NetworkDownloadTaskOperation method cancelByProducingResumeData:.

didWriteDataHandler

The method that will be called with as the data is being downloaded.

didFinishDownloadingHandler

The block that will be called when the upload is done.

Return Value

Returns NetworkDownloadTaskOperation.

Discussion

Note: The progress/completion blocks will, by default, be called on the main queue. If you want to use a different GCD queue, specify a non-nil completionQueue value.

Declared In

NetworkManager.h

downloadOperationWithURL:didWriteDataHandler:didFinishDownloadingHandler:

Create download task operation.

- (NetworkDownloadTaskOperation *)downloadOperationWithURL:(NSURL *)url didWriteDataHandler:(DidWriteDataHandler)didWriteDataHandler didFinishDownloadingHandler:(DidFinishDownloadingHandler)didFinishDownloadingHandler

Parameters

url

The NSURL.

didWriteDataHandler

The method that will be called with as the data is being downloaded.

didFinishDownloadingHandler

The block that will be called when the upload is done.

Return Value

Returns NetworkDownloadTaskOperation.

Discussion

Note: The progress/completion blocks will, by default, be called on the main queue. If you want to use a different GCD queue, specify a non-nil completionQueue value.

Declared In

NetworkManager.h

initWithSessionConfiguration:

Create session manager using the supplied NSURLSessionConfiguration

- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration

Parameters

configuration

The NSURLSessionConfiguration for the underlying NSURLSession.

Return Value

A session manager.

Declared In

NetworkManager.h

networkQueue

Operation queue for network requests.

- (NSOperationQueue *)networkQueue

Return Value

An NSOperationQueue. This will instantiate a queue if one hadn’t already been created.

Discussion

If you want, you can add operations to the NSURLSessionManager-provided operation queue. This method is provided in case you want to customize the queue or add operations to it yourself.

Declared In

NetworkManager.h

uploadOperationWithRequest:data:didSendBodyDataHandler:didCompleteWithDataErrorHandler:

Create upload task operation.

- (NetworkUploadTaskOperation *)uploadOperationWithRequest:(NSURLRequest *)request data:(NSData *)data didSendBodyDataHandler:(DidSendBodyDataHandler)didSendBodyDataHandler didCompleteWithDataErrorHandler:(DidCompleteWithDataErrorHandler)didCompleteWithDataErrorHandler

Parameters

request

The NSURLRequest.

data

The body of the request

didSendBodyDataHandler

The method that will be called with periodic updates while data is being uploaded

didCompleteWithDataErrorHandler

The block that will be called when the upload is done.

Return Value

Returns NetworkUploadTaskOperation.

Discussion

Note: The progress/completion blocks will, by default, be called on the main queue. If you want to use a different GCD queue, specify a non-nil completionQueue value.

Declared In

NetworkManager.h

uploadOperationWithRequest:fileURL:didSendBodyDataHandler:didCompleteWithDataErrorHandler:

Create upload task operation.

- (NetworkUploadTaskOperation *)uploadOperationWithRequest:(NSURLRequest *)request fileURL:(NSURL *)fileURL didSendBodyDataHandler:(DidSendBodyDataHandler)didSendBodyDataHandler didCompleteWithDataErrorHandler:(DidCompleteWithDataErrorHandler)didCompleteWithDataErrorHandler

Parameters

request

The NSURLRequest.

fileURL

The URL of the file to be uploaded

didSendBodyDataHandler

The method that will be called with periodic updates while data is being uploaded

didCompleteWithDataErrorHandler

The block that will be called when the upload is done.

Return Value

Returns NetworkUploadTaskOperation.

Discussion

Note: The progress/completion blocks will, by default, be called on the main queue. If you want to use a different GCD queue, specify a non-nil completionQueue value.

Declared In

NetworkManager.h

uploadOperationWithURL:data:didSendBodyDataHandler:didCompleteWithDataErrorHandler:

Create upload task operation.

- (NetworkUploadTaskOperation *)uploadOperationWithURL:(NSURL *)url data:(NSData *)data didSendBodyDataHandler:(DidSendBodyDataHandler)didSendBodyDataHandler didCompleteWithDataErrorHandler:(DidCompleteWithDataErrorHandler)didCompleteWithDataErrorHandler

Parameters

url

The NSURL.

data

The body of the request

didSendBodyDataHandler

The method that will be called with periodic updates while data is being uploaded

didCompleteWithDataErrorHandler

The block that will be called when the upload is done.

Return Value

Returns NetworkUploadTaskOperation.

Discussion

Note: The progress/completion blocks will, by default, be called on the main queue. If you want to use a different GCD queue, specify a non-nil completionQueue value.

Declared In

NetworkManager.h

uploadOperationWithURL:fileURL:didSendBodyDataHandler:didCompleteWithDataErrorHandler:

Create upload task operation.

- (NetworkUploadTaskOperation *)uploadOperationWithURL:(NSURL *)url fileURL:(NSURL *)fileURL didSendBodyDataHandler:(DidSendBodyDataHandler)didSendBodyDataHandler didCompleteWithDataErrorHandler:(DidCompleteWithDataErrorHandler)didCompleteWithDataErrorHandler

Parameters

url

The NSURL.

fileURL

The URL of the file to be uploaded

didSendBodyDataHandler

The method that will be called with periodic updates while data is being uploaded

didCompleteWithDataErrorHandler

The block that will be called when the upload is done.

Return Value

Returns NetworkUploadTaskOperation.

Discussion

Note: The progress/completion blocks will, by default, be called on the main queue. If you want to use a different GCD queue, specify a non-nil completionQueue value.

Declared In

NetworkManager.h