- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从网上下载一个文件。文件有时很大,有时可能会达到 100MB,我想在应用程序进入后台或设备锁定时继续下载。为此,我正在使用 AFNetworking 3.0
[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:myIdentifier];
只要我在 WiFi 上,它就可以正常工作。当我关闭 WiFi 并打开我的 4G 蜂窝网络时,它停止响应,并且由于我的下载请求,我没有收到任何数据。如果我使用
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
一切都很好,除了当应用程序进入后台时我的下载不会继续。
我还检查了 NSURLSessionConfiguration
和 NSURLRequest
上的 allowsCellularAccess
和 YES
对象,但我的下载确实在蜂窝网络上不工作。
这是我的完整代码
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/
我已经实现了以 20 个为一组下载大小为 1-2 MB 的 block 的代码。 session 完成前 20 个下载请求后,它将在 session 中设置新的 20 个请求。我已经在控制台日志中的真
找不到关于此实例变量的任何文档。 即使应用已移至后台,它是否与能够处理对网络请求的响应有关? 最佳答案 来自 SDK 的 NSURLSession 类注释: “为创建的任何 tcp 套接字启用扩展后台
我正在构建一个应用程序,用户可以在其中从应用程序和共享扩展启动后台文件上传。用户应该能够监控从主应用程序上传的任何进度。 在从扩展程序启动上传的情况下,我需要创建一个后台 session 配置,其 i
是否可以忽略设备网络设置中的代理设置,而不是在使用 NSURLSession 时不使用代理? 基本上,如果连接失败,我想退回到不使用代理。 谢谢! 最佳答案 我非常确定在 session 配置中提供一
我正在从网上下载一个文件。文件有时很大,有时可能会达到 100MB,我想在应用程序进入后台或设备锁定时继续下载。为此,我正在使用 AFNetworking 3.0 [NSURLSessionConfi
授权 header 在 NSURLSessionConfiguration 中设置,但是它没有附加到 NSURLSessionDataTask。这是 Foundation framework 中的错误
我正在尝试运行后台 NSURLSessionDownloadTask。但是,我注意到在任务实际开始之前大约需要 30 秒。当我使用 defaultSessionConfiguration 时,任务几乎
当 session 设置为在后台工作时(通过 backgroundSessionConfiguration 调用),NSURLSessionConfiguration 的新超时属性似乎不起作用。 有人
我正在尝试用 NSUrlSession 替换一些古老的网络代码,但是将 HTTPMaximumConnectionsPerHost 设置为 1 没有任何效果。这个请求代码被调用了 170 次,但它在任
我在官方文档中看到,NSURLSessionConfiguration 和 NSURLRequest 共享一些相同或相似的属性。比如NSURLRequestCachePolicy、networkSer
即使在设置缓存策略之后,我的 NSURLSession 仍在加载缓存数据: let url = NSURL(string: urlString); var sessionConfig = NSURLS
我正在使用代码: let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.r
我遵循了本教程 Downloading files in background with URLSessionDownloadTask 还有这个苹果文档 Downloading Files in th
从一开始,我是如何配置NSURLSession的: auto securityPolicy = self.securityPolicy; NSURLSessionConfiguration *conf
我想从 NSObject 获取服务器响应,当我收到响应时,它已返回到 viewController。因为我在服务器调用 NSObject 类时实现,然后我调用NSObject 方法,但在服务器响应之前
有了新的 NSURLSession,NSURLSessionConfiguration 对象上现在有一个 timeoutIntervalForRequest 用于创建 session 。 但是 NSU
我正在构建一个 iOS 应用程序,它在后台将一堆文件上传到 Amazon S3 - 我正在使用 NSURLSession 结合 NSURLSessionConfiguration 为此和这似乎运作良好
我正在使用 NSURLSession 进行 REST 网络服务调用。我已经在 NSURLSessionConfiguration.HTTPAdditionalHeaders 中设置了网络服务的内容类型
此方法设置背景对象。 - (void) downloadWithURL: (NSMutableArray *)urlArray pathArr: (NSMutableArra
我用一行代码启动了一个新的 Swift 应用程序: 让 config = NSURLSessionConfiguration.defaultSessionConfiguration() 和链接的二进制
我是一名优秀的程序员,十分优秀!