KMAdvertisementRequestServer.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /*
  102. status
  103. AFNetworkReachabilityStatusUnknown = -1, 不知道监测的是什么
  104. AFNetworkReachabilityStatusNotReachable = 0, 没有检测到网络
  105. AFNetworkReachabilityStatusReachableViaWWAN = 1, 蜂窝网
  106. AFNetworkReachabilityStatusReachableViaWiFi = 2, WIFI
  107. */
  108. func reachabilityStatusChange(block: @escaping(AFNetworkReachabilityStatus) ->Void) {
  109. // 1.创建网络监测单例
  110. let manager = AFNetworkReachabilityManager.shared()
  111. manager.setReachabilityStatusChange({ status in
  112. // 2.实现网络监测的回调
  113. block(status)
  114. })
  115. // 3.开始监测
  116. manager.startMonitoring()
  117. }
  118. }