我想下载使用NSUrlSession文件的列表。的iOS 7 NSURLSession下载多个文件在后台
我有一个计算成功下载@property (nonatomic) int downloadsSuccessfulCounter;
的变量。当正在下载的文件,我禁用Download Button
。当计数器等于下载列表大小,我再次启用按钮和计数器设置为0。我的方法做到这一点:
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
...
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
downloadsSuccessfulCounter++;
if(downloadsSuccessfulCounter == self.downloadList.count) {
NSLog(@"All downloads finished");
[self.syncButton setEnabled:YES];
downloadsSuccessfulCounter = 0;
}
}];
}
一切工作正常,但是当我再次打开ViewController我收到消息A background URLSession with identifier com.myApp already exists!
。计数器未设置为0,UI元素(UIButtons,UILabels)没有响应。
我想这个问题是因为NSURLSession仍然是开放的,但我真的不知道它是如何工作的。
我试过所有的教程,但其中99%仅用于下载1个文件,不超过1个... 任何想法?
这里是我的代码:
...
@property (nonatomic, strong) NSURLSession *session;
...
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.downloadList = [[NSMutableArray alloc] init];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.myApp"];
sessionConfiguration.HTTPMaximumConnectionsPerHost = 5;
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
}
当我按下Download Button
我调用这个方法( 我有一个Downloadable
对象,它包含一个NSURLSessionDownloadTask
):
-(void)startDownload {
for (int i=0; i<[self.downloadList count]; i++) {
Downloadable *d = [self.downloadList objectAtIndex:i];
if (!d.isDownloading) {
if (d.taskIdentifier == -1) {
d.downloadTask = [self.session downloadTaskWithURL:[NSURL URLWithString:d.downloadSource]];
}else {
d.downloadTask = [self.session downloadTaskWithResumeData:fdi.taskResumeData];
}
d.taskIdentifier = d.downloadTask.taskIdentifier;
[d.downloadTask resume];
d.isDownloading = YES;
}
}
}
当应用程序是背景:
-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
if ([downloadTasks count] == 0) {
if (appDelegate.backgroundTransferCompletionHandler != nil) {
void(^completionHandler)() = appDelegate.backgroundTransferCompletionHandler;
appDelegate.backgroundTransferCompletionHandler = nil;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
completionHandler();
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = @"All files downloaded";
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}];
}
}
}];
}
我目前也在探索这个......问题是会话标识符 - 它们必须对每个后续任务都是唯一的。根据此文本<<注意:您必须为每个标识符创建一个会话(在创建配置对象时指定)。共享相同标识符的多个会话的行为未定义。 >>本页来自:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html – 2014-10-19 21:24:55
我猜有两个选择我可以看到:1)编译所有内容到一个zip文件,下载单个文件,然后将其解压到客户端(电话)一侧 - 或者2)创建一个NSMutableArray任务,在运行时为每个文件添加一个新的后台会话并带有唯一标识符。 – 2014-10-19 21:26:27
本教程似乎有多个文件的前提:http://code.tutsplus.com/tutorials/working-with-nsurlsession-part-3--mobile-21879 – 2014-10-19 21:33:43