RANetworkTaskDelegate.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // JKNetworkSessionDelegate.m
  3. // Test Upload Progress
  4. //
  5. // Created by Jack on 2017/1/11.
  6. // Copyright © 2017年 mini1. All rights reserved.
  7. //
  8. #import "RANetworkTaskDelegate.h"
  9. @interface JLFileStream : NSObject
  10. @property (nonatomic,copy) NSString *filePath;
  11. @property (nonatomic,strong) NSInputStream *inputStream;
  12. @property (nonatomic,strong) NSOutputStream *outputStream;
  13. @end
  14. @implementation JLFileStream
  15. #pragma mark - life
  16. + (instancetype)jl_fileStreamOfPath:(NSString *)path {
  17. JLFileStream *stream = [[JLFileStream alloc] init];
  18. stream.filePath = path;
  19. return stream;
  20. }
  21. - (void)setFilePath:(NSString *)filePath {
  22. _filePath = filePath;
  23. if (!filePath) {
  24. return;
  25. }
  26. if (_outputStream) {
  27. [_outputStream close];
  28. }
  29. _outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
  30. [_outputStream open];
  31. }
  32. #pragma mark - out put stream
  33. - (void)jl_writeData:(NSData *)data {
  34. [self writeData:data toPath:self.filePath];
  35. }
  36. - (void)writeData:(NSData *)data toPath:(NSString *)path {
  37. [self.outputStream write:data.bytes maxLength:data.length];
  38. }
  39. - (void)jl_closeWriter {
  40. [self.outputStream close];
  41. }
  42. @end
  43. @interface RANetworkTaskDelegate ()
  44. @property (nonatomic,strong) NSMutableData *recvData;
  45. //@property (nonatomic,strong) NSMutableDictionary *result;
  46. @property (nonatomic,strong) JLFileStream *fileStream;
  47. @end
  48. @implementation RANetworkTaskDelegate
  49. + (instancetype)sharedInstance {
  50. RANetworkTaskDelegate *obj = nil;
  51. obj = [[RANetworkTaskDelegate alloc] init];
  52. obj.recvData = [NSMutableData data];
  53. // obj.result = [[JKNetworkResult alloc] init];
  54. printf("new network delegate\n");
  55. return obj;
  56. }
  57. #pragma mark - NSURLSessionTaskDelegate
  58. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
  59. double progress = (double)totalBytesSent / totalBytesExpectedToSend;
  60. if (self.p) {
  61. self.p(task,progress);
  62. }
  63. }
  64. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
  65. // if (self.decryptHandler) {
  66. //
  67. // self.result.decryptHandler = self.decryptHandler;
  68. //
  69. // }
  70. //
  71. // if (error) {
  72. //
  73. // self.result.error = error;
  74. //
  75. // NSLog(@"Delegate recv Error: %@",error.localizedDescription);
  76. //
  77. // }
  78. //
  79. // self.result.data = self.recvData;
  80. //
  81. // NSLog(@"Delegate recv data %@",self.recvData);
  82. //
  83. //
  84. //
  85. if (self.isDownloadTask) {
  86. [self.fileStream jl_closeWriter];
  87. }
  88. printf("net work complete\n");
  89. if (self.r) {
  90. if(self.recvData==nil)
  91. {
  92. self.r(nil);
  93. return;
  94. }
  95. NSMutableString *str = [[NSMutableString alloc] initWithData:self.recvData encoding:NSUTF8StringEncoding];
  96. NSLog(@"data string: %@",str);
  97. NSError *error1=nil;
  98. NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:self.recvData options:NSJSONReadingMutableLeaves error:&error1];
  99. if(jsobj==nil)// 服务器返回不是json
  100. {
  101. jsobj=[NSMutableDictionary new];
  102. [jsobj setValue:@"-30" forKey:@"result"];
  103. [jsobj setValue:@"Can not upload to server, please contact administrator." forKey:@"msg"];
  104. }
  105. self.r([jsobj mutableCopy]);
  106. }
  107. //
  108. [session invalidateAndCancel];
  109. }
  110. #pragma mark - NSURLSessionDataDelegate
  111. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
  112. NSLog(@"Delegate recv response: %@",response);
  113. if (self.isDownloadTask) {
  114. // 文件保存地址
  115. if (!self.fileCachePath) {
  116. self.fileCachePath = NSTemporaryDirectory();
  117. }
  118. NSString *desPath = self.fileCachePath;
  119. NSError *err;
  120. BOOL isDir = NO;
  121. [[NSFileManager defaultManager] fileExistsAtPath:desPath isDirectory:&isDir];
  122. if (isDir) {
  123. desPath = [self.fileCachePath stringByAppendingPathComponent:response.suggestedFilename];
  124. }
  125. // 如果文件存在
  126. if ([[NSFileManager defaultManager] fileExistsAtPath:desPath]) {
  127. [[NSFileManager defaultManager] removeItemAtPath:desPath error:&err];
  128. if (err) {
  129. completionHandler(NSURLSessionResponseCancel);
  130. return;
  131. }
  132. }
  133. self.fileCachePath = desPath;
  134. self.fileStream = [JLFileStream jl_fileStreamOfPath:self.fileCachePath];
  135. }
  136. // self.result.response = response;
  137. completionHandler(NSURLSessionResponseAllow);
  138. }
  139. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
  140. if (!self.isDownloadTask) {
  141. [self.recvData appendData:data];
  142. } else {
  143. [self.fileStream jl_writeData:data];
  144. }
  145. }
  146. @end