ios – 奇怪的NSURLSessionDownloadTask行为超过蜂窝(不是wifi)

前端之家收集整理的这篇文章主要介绍了ios – 奇怪的NSURLSessionDownloadTask行为超过蜂窝(不是wifi)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当应用程序收到推送通知时,我已经启用了具有远程通知任务的后台模式,以便在后台下载一个小文件(100kb).
我已使用配置下载会话
NSURLSessionConfiguration *backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:sessionIdentifier];
[backgroundConfiguration setAllowsCellularAccess:YES];


self.backgroundSession = [NSURLSession sessionWithConfiguration:backgroundConfiguration
                                                       delegate:self
                                                  delegateQueue:[NSOperationQueue mainQueue]];

并使用它激活它

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[hostComponents URL]];

[request setAllowsCellularAccess:YES];


NSMutableData *bodyMutableData = [NSMutableData data];
[bodyMutableData appendData:[params dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[bodyMutableData copy]];


_downloadTask =  [self.backgroundSession downloadTaskWithRequest:request];

[self.downloadTask resume];

现在一切都可以正常工作,只有当我通过Wifi连接或超过蜂窝,但iPhone连接到xCode的电缆,如果我断开iPhone,并通过蜂窝接收推送通知代码停止在[self.downloadTask resume];线不调用URL.

处理这些操作的类是NSURLSessionDataDelegate,NSURLSessionDownloadDelegate,NSURLSessionTaskDelegate等实现:

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition,NSURLCredential *))completionHandler

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location

我尝试在[self.downloadTask resume]之后插入一个UILocalNotification(现在显示为’now)的调试行,但在5分钟后调用,并指出self.downloadTask.state被“暂停”

什么是这个奇怪的行为?

解决方法

NSURLSessionConfiguration类参考文档在这里:

https://developer.apple.com/Library/ios/documentation/Foundation/Reference/NSURLSessionConfiguration_class/Reference/Reference.html#//apple_ref/occ/instp/NSURLSessionConfiguration/discretionary

说:自由财产:

@H_404_29@Discussion

When this flag is set,transfers are more likely to occur when plugged
into power and on Wi-Fi. This value is false by default.

This property is used only if a session’s configuration object was
originally constructed by calling the backgroundSessionConfiguration:
method,and only for tasks started while the app is in the foreground.
@H_404_29@If a task is started while the app is in the background,that task is
treated as though discretionary were true,regardless of the actual
value of this property. For sessions created based on other
configurations,this property is ignored.

这似乎意味着如果下载在后台启动,操作系统总是有权决定是否和何时进行下载.在完成这些任务之前,操作系统总是等待WiFi连接.

我的经验支持这个猜想.我发现,当设备处于蜂窝状态时,我可以发送几个下载通知.他们仍然卡住当我将设备切换到wifi时,他们都会通过.

原文链接:https://www.f2er.com/iOS/330253.html

猜你在找的iOS相关文章