gpt4 book ai didi

ios - 蜂窝网络上的 NSURLSessionConfiguration 后台任务未下载

转载 作者:行者123 更新时间:2023-11-28 21:29:30 26 4
gpt4 key购买 nike

我正在从网上下载一个文件。文件有时很大,有时可能会达到 100MB,我想在应用程序进入后台或设备锁定时继续下载。为此,我正在使用 AFNetworking 3.0

[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:myIdentifier];

只要我在 WiFi 上,它就可以正常工作。当我关闭 WiFi 并打开我的 4G 蜂窝网络时,它停止响应,并且由于我的下载请求,我没有收到任何数据。如果我使用

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

一切都很好,除了当应用程序进入后台时我的下载不会继续。

我还检查了 NSURLSessionConfigurationNSURLRequest 上的 allowsCellularAccessYES 对象,但我的下载确实在蜂窝网络上不工作。

这是我的完整代码

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


}];

最佳答案

你不应该设置自由裁量权标志。这告诉操作系统等待下载数据,直到方便的时间(IIRC,这基本上意味着设备 A. sleep ,B. 通电,C. 连接到 Wi-Fi)。

关于ios - 蜂窝网络上的 NSURLSessionConfiguration 后台任务未下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36739217/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com