我在旧的 AFNetworking 中有一个选项来设置 outputStream:
AFHTTPRequestOperation* requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
requestOperation.outputStream = [NSOutputStream outputStreamToFileAtPath:somePath append:NO];
如何在 AFNetworking 3.0 中实现这一点?
Best Answer-推荐答案 strong>
在 AFHTTPSessionManager 上设置 setDataTaskDidReceiveDataBlock 以将数据写入缓冲区。然后使用 NSOutputStream 将该缓冲区写入文件。
NSString *baseURLString = @"https://www.whatever.com";
NSString *pathString = "lol/rofl/";
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:baseURLString]];
manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject"text/plain"];
[manager setDataTaskDidReceiveDataBlock:^(NSURLSession * _Nonnull session, NSURLSessionDataTask * _Nonnull dataTask, NSData * _Nonnull data) {
[self.dataBuffer appendBytes:[data bytes] length:data.length];
}];
[manager GET:pathString parameters:nil progress:...]
然后引用此 Apple 指南使用 NSOutputStream 和下面的流委托(delegate)回调方法将数据写入文件:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Streams/Articles/WritingOutputStreams.html
- (void)streamNSStream *)stream handleEventNSStreamEvent)eventCode
关于ios - 如何在 AFNetworking 3.0 中设置 outputStream,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/40973536/
|