D
D
deleted-mezhevikin2013-11-05 17:04:41
iOS
deleted-mezhevikin, 2013-11-05 17:04:41

How to resume file uploads in NSURLSession after application restart?

I create several downloadTasks and then kill the application.
I launch the application and get an array with unfinished tasks, after which I resume loading all in a loop.
But the files never show up in the documents folder. (if you don't reload applications, the download itself works)
I'm using AFNetworking 2.0.2
The full code for this project is github.com/nullproduction/FileDownloader

//
//  DownloadsViewController.m
//

#import "DownloadsViewController.h"

@implementation DownloadsViewController

- (id)init
{
    [self printDocumentsPath];
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"downloads"];
    
    manager = ;
        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

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timokhin, 2013-11-06
@timokhin

Try to resume operations
In AppDelegate.m
To pause operations

- (void)applicationDidEnterBackground:(UIApplication *)application

To resume operations
- (void)applicationWillEnterForeground:(UIApplication *)application

developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question