以前在iOS7中,当我们试图在后台使用流请求上载时,我们会得到以下异常
由于异常“NSGenericException”终止应用程序,原因:“后台会话中的上载任务必须来自文件”
但是在iOS8中,当我们尝试在后台使用流上传时也不例外。
现在我的问题是
( 1) uploadTaskWithStreamedRequest:是否允许在iOS8中进行葫芦上传?
2)在uploadTaskWithStreamedRequest.中,我使用的是背景NSURLConfiguration我使用-(void)URLSession:(NSURLSession *)会话任务:(NSURLSessionTask*) needNewBodyStream:(void (^)(NSInputStream *))completionHandler向NSUrlSession提供流。当应用程序在前台时,它运行良好,并将我的文件上传到服务器。但是,一旦应用程序在后台运行,流就会结束,NSURLSession就会出现以下错误
错误Domain=NSURLErrorDomain代码=-997“与后台传输服务失去连接”
我认为当应用程序进入后台时,我的流就结束了。现在我的问题是,在哪一个运行循环中,我应该安排我的流,或者让我知道在我的理解中是否有任何错误。
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task needNewBodyStream:(void (^)(NSInputStream *))completionHandler
{
// Open producer/consumer streams. We open the producerStream straight
// away. We leave the consumerStream alone; NSURLConnection will deal
// with it.
NSLog(@"%@", [NSThread currentThread]);
NSInputStream *consStream;
NSOutputStream *prodStream;
[NSStream createBoundInputStream:&consStream outputStream:&prodStream bufferSize:SFAMaxBufferLength];
assert(consStream != nil);
assert(prodStream != nil);
self.consumerStream = consStream;
self.producerStream = prodStream;
self.producerStream.delegate = self;
[self.producerStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[self.producerStream open];
// Set up our state to send the body prefix first.
self.buffer = [self.bodyPrefixData bytes];
self.bufferLimit = [self.bodyPrefixData length];
completionHandler(self.consumerStream);
}
发布于 2015-02-10 08:41:40
不能使用后台配置上载流任务。我仅在两种情况下成功地上传了数据:
发布于 2015-08-17 20:34:03
你可以上传一个多部分文件的背景-只是这不是直接。参考:AFNetworking error in uploadTaskWithStreamedRequest
https://stackoverflow.com/questions/28315141
复制