- (NSURLSession *)sharedBackgroundSession
{
static NSURLSession *sharedBackgroundSession = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.example.apple-samplecode.SimpleBackgroundTransfer.BackgroundSession"];
configuration.URLCache = [NSURLCache sharedURLCache];
configuration.requestCachePolicy = NSURLRequestReturnCacheDataElseLoad;
sharedBackgroundSession = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
});
return sharedBackgroundSession;
}
// When executed on simulator, things work as expected, I never get a nil result.
// When executed on iPhone 5s device, nextDownloadTask is always nil
NSURLSessionDownloadTask *nextDownloadTask = [[self sharedBackgroundSession] downloadTaskWithRequest:request];
Any ideas?
UPDATE 9/12/14:
I had this issue again, googled and found this SO post and then saw that I was the author of the question! This time around -URLSession:task:didCompleteWithError
wasn't even getting called. THE SOLUTION WAS TO RESTART MY PHONE. This must be an Apple bug. I'm running iOS7 on my iPhone 5s.