deleted-mezhevikin
@deleted-mezhevikin

Как возобновить загрузку файлов в NSURLSession после перезагрузки приложения?

Я создаю несколько downloadTask, после чего убиваю приложение.

Запускаю приложение и получаю массив с незавершенными задачами, после чего в цикле возобновляю загрузку всех.

Но файлы так и не появляются в папке documets. (если не перезагружать приложения, то сама по себе загрузка работает)


Я использую AFNetworking 2.0.2

Полный код этого проекта — github.com/nullproduction/FileDownloader

//
//  DownloadsViewController.m
//

#import "DownloadsViewController.h"

@implementation DownloadsViewController

- (id)init
{
    [self printDocumentsPath];
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"downloads"];
    
    manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    NSArray *downloadTasks = [manager downloadTasks];
    
    if (downloadTasks.count)
    {
        NSLog(@"downloadTasks: %@", downloadTasks);
        // resume all
        for (NSURLSessionDownloadTask *downloadTask in downloadTasks)
        {
            [downloadTask resume];
        }
    }
    
    return [super init];
}

- (void)addDownloadTask:(id)sender
{
    NSURL *URL = [NSURL URLWithString:@"http://www.rarlab.com/rar/wrar500.exe"];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request
    progress:nil
    destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
       NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
        return [documentsDirectoryPath URLByAppendingPathComponent:[targetPath lastPathComponent]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
         NSLog(@"File downloaded to: %@", filePath);
    }];
    [downloadTask resume];
    
    NSLog(@"%d", downloadTask.taskIdentifier);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"addDownloadTask" style:UIBarButtonItemStyleBordered target:self action:@selector(addDownloadTask:)];
}

- (void)printDocumentsPath
{
    NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSLog(@"%@", documentsDirectoryPath);
}



@end
  • Вопрос задан
  • 3229 просмотров
Пригласить эксперта
Ответы на вопрос 1
timokhin
@timokhin
iOS developer
Попробуйте возобновлять операции
В AppDelegate.m

Для приостановки операций
- (void)applicationDidEnterBackground:(UIApplication *)application


Для возобновления операций
- (void)applicationWillEnterForeground:(UIApplication *)application


developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы