Ich lade eine Datei aus dem Internet. Die Dateigröße ist manchmal sehr groß und kann bis zu 100MBs erreichen. Ich möchte den Download fortsetzen, während die App im Hintergrund läuft oder wenn das Gerät gesperrt ist. Dafür bin ich mit AFNetworking 3.0
NSURLSessionConfiguration Hintergrund Task auf Mobilfunknetz nicht herunterladen
[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:myIdentifier];
Es funktioniert gut, solange ich auf WiFi bin. Wenn ich WLAN ausschalte und mein Mobilfunknetz, das 4G ist, einschalte, reagiert es nicht mehr und ich erhalte keine Daten aufgrund meiner Download-Anfrage. Wenn i
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
verwenden alles war in Ordnung, außer meinen Download wird nicht fortgesetzt, wenn die App in den Hintergrund geht.
Ich habe überprüft auch allowsCellularAccess
auf NSURLSessionConfiguration
und NSURLRequest
und Objekt, das YES
ist, aber mein Download, wenn sie auf Mobilfunknetz funktioniert nicht.
Hier ist mein vollständiger Code
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]);
}];