afnetworkingnsurlsessionconfiguration

AFNetworking 3.0 download background mode


I use AFNetworking library 3.0 for download files. I created singleton for AFNetworking session but when my phone go to lockscreen session has been canceled. Also my project has accept "Background fetch mode" What i am doing wrong? Thanks for help! My code below:

-(void)downloadShowingProgress:(NSString *)url nameString:(NSString *)nameString indexPath:(NSIndexPath *)indexPath{

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURL *URL = [NSURL URLWithString:url];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    self.downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSURL *directoryURL = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        NSURL *documentsDirectoryURL = [directoryURL URLByAppendingPathComponent:@".fade"];
        NSError *error;
        if ([fileManager createDirectoryAtURL:documentsDirectoryURL withIntermediateDirectories:YES attributes:nil error:&error]){
            NSLog(@"Create Sucess");
        }
        else{
            NSLog(@"Create error: %@", error);
        }

        return [documentsDirectoryURL URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3",nameString]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        NSLog(@"** File downloaded to: %@", filePath);

    }];

    [self.downloadTask resume];

}

Solution

  • you must be add backgroundSessionConfigurationWithIdentifier like that:

    -(void)downloadShowingProgress:(NSString *)url nameString:(NSString *)nameString indexPath:(NSIndexPath *)indexPath{
    
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"download-task"];
        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
        NSURL *URL = [NSURL URLWithString:url];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
        self.downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    
            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSURL *directoryURL = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
            NSURL *documentsDirectoryURL = [directoryURL URLByAppendingPathComponent:@".fade"];
            NSError *error;
            if ([fileManager createDirectoryAtURL:documentsDirectoryURL withIntermediateDirectories:YES attributes:nil error:&error]){
                NSLog(@"Create Sucess");
            }
            else{
                NSLog(@"Create error: %@", error);
            }
    
            return [documentsDirectoryURL URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3",nameString]];
        } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
            NSLog(@"** File downloaded to: %@", filePath);
    
        }];
    
        [self.downloadTask resume];
    
    }