- (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];
有任何想法吗?
2014 年 9 月 12 日更新:我又遇到了这个问题,谷歌搜索并找到了这个 SO 帖子,然后看到我是这个问题的作者!这一次-URLSession:task:didCompleteWithError
甚至没有被召唤。解决方案是重新启动我的手机。这一定是苹果的错误。我在我的 iPhone 5s 上运行 iOS7。