我想下載使用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