KMAdvertisementRequestServer.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // KMAdvertisementRequestServer.swift
  3. // KMAdvertisement
  4. //
  5. // Created by lizhe on 2022/11/23.
  6. //
  7. import AFNetworking
  8. typealias KMAdvertisementRequestServerCompletion = (_ task: URLSessionDataTask?, _ responseObject: AnyObject?, _ error: Error?) -> Void
  9. class KMAdvertisementRequestServer {
  10. var sessionManager: AFHTTPSessionManager!
  11. static let requestServer = KMAdvertisementRequestServer()
  12. init() {
  13. let configuration: URLSessionConfiguration = URLSessionConfiguration.default
  14. sessionManager = AFHTTPSessionManager.init(sessionConfiguration: configuration)
  15. sessionManager.securityPolicy = AFSecurityPolicy.default()
  16. }
  17. func request(urlString: String,
  18. method:String,
  19. params: Dictionary<String, Any>?,
  20. completion: @escaping KMAdvertisementRequestServerCompletion) -> Void {
  21. self.request(urlString: urlString,
  22. method: method,
  23. params: params,
  24. requestSerializer: nil,
  25. completion: completion)
  26. }
  27. func request(urlString: String,
  28. method:String,
  29. params: Dictionary<String, Any>?,
  30. requestSerializer: ((_ requestSerializer:AFHTTPRequestSerializer) -> Void)?,
  31. completion: @escaping KMAdvertisementRequestServerCompletion) -> Void {
  32. self.request(urlString: urlString,
  33. method: method,
  34. params: params,
  35. requestSerializer: requestSerializer,
  36. body: nil,
  37. completion: completion)
  38. }
  39. func request(urlString: String,
  40. method:String,
  41. params: Dictionary<String, Any>?,
  42. requestSerializer: ((_ requestSerializer:AFHTTPRequestSerializer) -> Void)?,
  43. body: ((_ formData:AFMultipartFormData) -> Void)?,
  44. completion: @escaping KMAdvertisementRequestServerCompletion) -> Void {
  45. //每次请求需要重新创建
  46. sessionManager.requestSerializer = AFJSONRequestSerializer()
  47. sessionManager.responseSerializer = AFJSONResponseSerializer()
  48. sessionManager.responseSerializer.acceptableContentTypes = ["application/json","text/html","text/json","text/javascript","text/plain","image/gif"]
  49. sessionManager.requestSerializer.timeoutInterval = 20
  50. if (requestSerializer != nil) {
  51. requestSerializer!(sessionManager.requestSerializer);
  52. }
  53. if (method == "POST") {
  54. if (body != nil) {
  55. sessionManager.post(urlString, parameters: params, headers: nil) { formData in
  56. body!(formData)
  57. } progress: { progress in
  58. } success: { task, responseObject in
  59. let responseObject = responseObject as AnyObject
  60. completion(task,responseObject,nil);
  61. } failure: { task, error in
  62. completion(task,nil,error);
  63. }
  64. } else {
  65. sessionManager.post(urlString, parameters: params, headers: nil) { progress in
  66. } success: { task, responseObject in
  67. let responseObject = responseObject as AnyObject
  68. completion(task,responseObject,nil)
  69. } failure: { task, error in
  70. completion(task,nil,error)
  71. }
  72. }
  73. } else if (method == "GET") {
  74. sessionManager.get(urlString, parameters: params, headers: nil) { progress in
  75. } success: { task, responseObject in
  76. let responseObject = responseObject as AnyObject
  77. completion(task,responseObject,nil)
  78. } failure: { task, error in
  79. completion(task,nil,error)
  80. }
  81. }
  82. }
  83. /**
  84. 下载
  85. */
  86. func downLoad(inputURL: URL,
  87. filePath: String,
  88. progress: @escaping(_ progress: Progress) -> Void,
  89. completion: @escaping KMAdvertisementRequestServerCompletion) {
  90. let request: URLRequest = URLRequest.init(url: inputURL)
  91. self.sessionManager.requestSerializer.timeoutInterval = 100.0
  92. let downloadTask = self.sessionManager.downloadTask(with: request) { resultProgress in
  93. progress(resultProgress)
  94. } destination: { url, response in
  95. return NSURL(fileURLWithPath: filePath) as URL
  96. } completionHandler: { resultResponse, resultUrl, error in
  97. completion(nil, resultResponse, error)
  98. }
  99. downloadTask.resume()
  100. // /* 下载地址 */
  101. // NSURL *url = [NSURL URLWithString:requestURL];
  102. // NSURLRequest *request = [NSURLRequest requestWithURL:url];
  103. // /* 下载路径 */
  104. // NSString *downFilePath = [NSString stringWithFormat:@"%@/%@", [self setPathOfDocumentsByFileCreated:created fileName:fileName], fileName];
  105. // self.downFilePath = downFilePath;
  106. //
  107. // /* 开始请求下载 */
  108. // self.downloadTask = [_sessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
  109. // float p = downloadProgress.fractionCompleted * 100;
  110. // if (progress) {
  111. // progress(p);
  112. // }
  113. // NSLog(@"下载进度:%.0f%", downloadProgress.fractionCompleted * 100);
  114. // } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  115. // dispatch_async(dispatch_get_main_queue(), ^{
  116. // //如果需要进行UI操作,需要获取主线程进行操作
  117. // });
  118. // /* 设定下载到的位置 */
  119. // return [NSURL fileURLWithPath:downFilePath];
  120. //
  121. // } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  122. // NSLog(@"下载完成");
  123. // if (error) {
  124. // if (failure) {
  125. // failure(error);
  126. // }
  127. // [weakSelf removeDownLoadFile];
  128. // } else {
  129. // if (success) {
  130. // success(downFilePath,[weakSelf setPathOfDocumentsByFileCreated:created fileName:fileName]);
  131. // }
  132. // }
  133. // }];
  134. // [self.downloadTask resume];
  135. }
  136. /*
  137. status
  138. AFNetworkReachabilityStatusUnknown = -1, 不知道监测的是什么
  139. AFNetworkReachabilityStatusNotReachable = 0, 没有检测到网络
  140. AFNetworkReachabilityStatusReachableViaWWAN = 1, 蜂窝网
  141. AFNetworkReachabilityStatusReachableViaWiFi = 2, WIFI
  142. */
  143. func reachabilityStatusChange(block: @escaping(AFNetworkReachabilityStatus) ->Void) {
  144. // 1.创建网络监测单例
  145. let manager = AFNetworkReachabilityManager.shared()
  146. manager.setReachabilityStatusChange({ status in
  147. // 2.实现网络监测的回调
  148. block(status)
  149. })
  150. // 3.开始监测
  151. manager.startMonitoring()
  152. }
  153. }