| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- //
- // JKNetworkSessionDelegate.m
- // Test Upload Progress
- //
- // Created by Jack on 2017/1/11.
- // Copyright © 2017年 mini1. All rights reserved.
- //
- #import "RANetworkTaskDelegate.h"
- @interface JLFileStream : NSObject
- @property (nonatomic,copy) NSString *filePath;
- @property (nonatomic,strong) NSInputStream *inputStream;
- @property (nonatomic,strong) NSOutputStream *outputStream;
- @end
- @implementation JLFileStream
- #pragma mark - life
- + (instancetype)jl_fileStreamOfPath:(NSString *)path {
-
- JLFileStream *stream = [[JLFileStream alloc] init];
- stream.filePath = path;
-
- return stream;
- }
- - (void)setFilePath:(NSString *)filePath {
- _filePath = filePath;
-
- if (!filePath) {
- return;
- }
-
- if (_outputStream) {
- [_outputStream close];
- }
-
- _outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
- [_outputStream open];
-
- }
- #pragma mark - out put stream
- - (void)jl_writeData:(NSData *)data {
-
- [self writeData:data toPath:self.filePath];
-
- }
- - (void)writeData:(NSData *)data toPath:(NSString *)path {
-
- [self.outputStream write:data.bytes maxLength:data.length];
-
- }
- - (void)jl_closeWriter {
-
- [self.outputStream close];
-
- }
- @end
- @interface RANetworkTaskDelegate ()
- @property (nonatomic,strong) NSMutableData *recvData;
- //@property (nonatomic,strong) NSMutableDictionary *result;
- @property (nonatomic,strong) JLFileStream *fileStream;
- @end
- @implementation RANetworkTaskDelegate
- + (instancetype)sharedInstance {
- RANetworkTaskDelegate *obj = nil;
- obj = [[RANetworkTaskDelegate alloc] init];
- obj.recvData = [NSMutableData data];
- // obj.result = [[JKNetworkResult alloc] init];
- printf("new network delegate\n");
- return obj;
- }
- #pragma mark - NSURLSessionTaskDelegate
- - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
-
- double progress = (double)totalBytesSent / totalBytesExpectedToSend;
-
- if (self.p) {
- self.p(task,progress);
- }
- }
- - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
-
- // if (self.decryptHandler) {
- //
- // self.result.decryptHandler = self.decryptHandler;
- //
- // }
- //
- // if (error) {
- //
- // self.result.error = error;
- //
- // NSLog(@"Delegate recv Error: %@",error.localizedDescription);
- //
- // }
- //
- // self.result.data = self.recvData;
- //
- // NSLog(@"Delegate recv data %@",self.recvData);
- //
- //
- //
-
- if (self.isDownloadTask) {
-
- [self.fileStream jl_closeWriter];
-
- if (error) {
- if (error.code != -999) {
- if (self.r) {
-
- NSMutableDictionary *resutlDic = [@{
- @"result" : @0,
- @"msg" : error.localizedDescription
- } mutableCopy];
- self.r(resutlDic);
- }
- } else {
-
- NSLog(@"download task canceled");
-
- }
-
- // 失败删除文件
- BOOL isDir = NO;
- if ([[NSFileManager defaultManager] fileExistsAtPath:self.fileCachePath isDirectory:&isDir]) {
-
- if (!isDir) {
- [[NSFileManager defaultManager] removeItemAtPath:self.fileCachePath error:nil];
- }
-
- }
-
- } else {
- if (self.r) {
-
- NSMutableDictionary *resutlDic = [@{
- @"result" : @2,
- @"path" : self.fileCachePath,
- @"msg" : @"download complete"
- } mutableCopy];
- self.r(resutlDic);
- }
- }
- } else {
-
- if (self.r) {
-
-
- if(self.recvData==nil)
- {
- self.r(nil);
- return;
-
- }
-
- NSMutableString *str = [[NSMutableString alloc] initWithData:self.recvData encoding:NSUTF8StringEncoding];
-
- NSLog(@"data string: %@",str);
-
- NSError *error1=nil;
- NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:self.recvData options:NSJSONReadingMutableLeaves error:&error1];
- if(jsobj==nil)// 服务器返回不是json
- {
-
- jsobj=[NSMutableDictionary new];
- [jsobj setValue:@"-30" forKey:@"result"];
- [jsobj setValue:@"Can not upload to server, please contact administrator." forKey:@"msg"];
-
-
-
- }
-
- self.r([jsobj mutableCopy]);
-
- }
- }
- printf("net work complete\n");
-
- //
- [session invalidateAndCancel];
- }
- #pragma mark - NSURLSessionDataDelegate
- - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
-
- NSLog(@"Delegate recv response: %@",response);
-
- if (self.isDownloadTask) {
-
- // 文件保存地址
- if (!self.fileCachePath) {
- self.fileCachePath = NSTemporaryDirectory();
- }
- NSString *desPath = self.fileCachePath;
- NSError *err;
- BOOL isDir = NO;
- [[NSFileManager defaultManager] fileExistsAtPath:desPath isDirectory:&isDir];
- if (isDir) {
- if (response.suggestedFilename) {
- desPath = [self.fileCachePath stringByAppendingPathComponent:response.suggestedFilename];
- } else {
- desPath = [self.fileCachePath stringByAppendingPathComponent:[NSUUID UUID].UUIDString];
- }
- }
-
- // 如果文件存在
- if ([[NSFileManager defaultManager] fileExistsAtPath:desPath]) {
-
- [[NSFileManager defaultManager] removeItemAtPath:desPath error:&err];
- if (err) {
- completionHandler(NSURLSessionResponseCancel);
- return;
- }
- }
-
- self.fileCachePath = desPath;
-
- self.fileStream = [JLFileStream jl_fileStreamOfPath:self.fileCachePath];
- }
-
- // self.result.response = response;
- completionHandler(NSURLSessionResponseAllow);
-
- }
- - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
-
- if (!self.isDownloadTask) {
- [self.recvData appendData:data];
- } else {
-
-
- if (self.p) {
- int64_t totalExpectedRevc = [dataTask countOfBytesExpectedToReceive];
- int64_t totalRevc = [dataTask countOfBytesReceived];
- double progress = (double)totalRevc / totalExpectedRevc;
- self.p(dataTask,progress);
- }
-
- [self.fileStream jl_writeData:data];
- }
- }
- @end
|