2016-12-05 61 views
2

我在舊的AFNetworking選項設置爲OutputStream有:如何設置的OutputStream在AFNetworking 3.0

AFHTTPRequestOperation* requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
requestOperation.outputStream = [NSOutputStream outputStreamToFileAtPath:somePath append:NO]; 

我怎樣才能做到這一點AFNetworking 3.0嗎?

+0

嘿,你可以接受的答案嗎? – lespommes

回答

2

在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:...] 

然後參考這個蘋果直營店使用一個NSOutputStream和下方的小河委託回調方法將數據寫入文件:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Streams/Articles/WritingOutputStreams.html

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode