【问题标题】:AFNetworking 3.0 download background modeAFNetworking 3.0 下载后台模式
【发布时间】:2019-08-01 08:09:00
【问题描述】:

我使用 AFNetworking 库 3.0 下载文件。我为 AFNetworking 会话创建了单例,但是当我的手机进入锁屏会话时已被取消。我的项目也接受了“后台获取模式” 我做错了什么?感谢帮助! 我的代码如下:

-(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];

}

【问题讨论】:

    标签: afnetworking nsurlsessionconfiguration


    【解决方案1】:

    你必须像这样添加 backgroundSessionConfigurationWithIdentifier:

    -(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];
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      相关资源
      最近更新 更多