From Using NSURLSession :
The NSURLSession class supports background hyphenation while your application is paused. Background transfers are provided only by sessions created using the background session configuration object (which is returned by calling backgroundSessionConfiguration: .
You must configure your AFHTTPSessionManager to use the background session configuration if you want to do this:
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.myApp.backgroundDownloadSession"] AFHTTPSessionManager *backgroundManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
AFNetworking will act as a delegate. From NSURLSession docs:
[T] The delegate will be saved until the delegate receives the message URLSession:didBecomeInvalidWithError:
As a result, your manager will stick to this session.
Two side notes:
You should probably use a separate AFHTTPSessionManager for background transfers (large downloads, etc.). You do not want literally all requests to have a background URL session assigned.
If you want a response without AFNetworking, note that the background session identifier ('com.myApp.backgroundDownloadSession' in my code example):
The identifier for the new session configuration that is unique to your application. Your application may receive a download or download response later by creating a new background session with the same identifier.
Aaron brager
source share