0

私はWebからファイルをダウンロードしています。ファイルサイズが大きく、何回か最大100MBに達することがあります。アプリをバックグラウンドにするか、デバイスがロックされている間、ダウンロードを続けたいと思います。このために私はそれがある限り、私は無線LANの午前として正常に動作AFNetworking 3.0NSURLSessionConfiguration携帯電話のバックグラウンドタスクがダウンロードされない

[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:myIdentifier];

を使用しています。 WiFiをオフにして4Gのセルラーネットワークをオンにすると、応答が停止し、ダウンロード要求の結果としてデータが取得されません。私は

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

を使用する場合はアプリがバックグラウンドになったときに、私のダウンロードは続行されませんを除いてすべてのものは大丈夫でした。

は、私はまた、NSURLSessionConfigurationNSURLRequestYESあるオブジェクトにallowsCellularAccessをチェックしましたが、私のダウンロードは、セルラーネットワーク上のときは動作しません。

は、ここに私の完全なコード

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:myIdentifier]; 
    configuration.discretionary = YES; 
    configuration.sessionSendsLaunchEvents = YES; 
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 
    NSURL *URL = [NSURL URLWithString:downloadUrl]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 
    NSLog(@"Allow Cellular Network : %d",request.allowsCellularAccess); 
    NSLog(@"Allow Cellular Network for session: %d",configuration.allowsCellularAccess); 
    NSLog(@"Resource timneout interval: %f",configuration.timeoutIntervalForResource); 
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self callProgressBlocksForUrl:lesson.archiveUrl withProgress:downloadProgress.fractionCompleted]; 
    }); 

    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { 
    NSLog(@"Getting Path for File saving"); 

return [NSURL fileURLWithPath:fullPath]; 
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { 

    NSHTTPURLResponse * myresponse = (NSHTTPURLResponse *)response; 
    NSLog(@"Video downloaded, headers: %@", [myresponse.allHeaderFields description]); 


    }]; 

答えて

1

あなたは裁量フラグを設定すべきではないです。これは、OSが便利な時間(IIRCは基本的にデバイスがA.スリープ、B.オン電源、C.がWi-Fiに接続されていることを意味する)までデータをダウンロードするのを待つように指示します。

関連する問題