iOS-study
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

NetworkManager.swift 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // NetworkManager.swift
  3. // iOSFirst
  4. //
  5. // Created by 孙宇峰 on 2023/2/22.
  6. //
  7. import Foundation
  8. import Moya
  9. import Alamofire
  10. import SwiftyJSON
  11. /// 超时时长
  12. private var requestTimeOut:Double = 30
  13. ///endpointClosure
  14. private let myEndpointClosure = { (target: AccountService) -> Endpoint in
  15. ///这里的endpointClosure和网上其他实现有些不太一样。
  16. ///主要是为了解决URL带有?无法请求正确的链接地址的bug
  17. let url = target.baseURL.absoluteString + target.path
  18. var endpoint = Endpoint(
  19. url: url,
  20. sampleResponseClosure: { .networkResponse(200, target.sampleData) },
  21. method: target.method,
  22. task: target.task,
  23. httpHeaderFields: target.headers
  24. )
  25. switch target {
  26. default:
  27. requestTimeOut = 30//设置默认的超时时长
  28. return endpoint
  29. }
  30. }
  31. private let requestClosure = { (endpoint: Endpoint, done: MoyaProvider.RequestResultClosure) in
  32. do {
  33. var request = try endpoint.urlRequest()
  34. //设置请求时长
  35. request.timeoutInterval = requestTimeOut
  36. // 打印请求参数
  37. if let requestData = request.httpBody {
  38. log.info("\(request.url!)"+"\n"+"\(request.httpMethod ?? "")"+"发送参数"+"\(String(data: request.httpBody!, encoding: String.Encoding.utf8) ?? "")")
  39. }else{
  40. log.info("\(request.url!)"+"\(String(describing: request.httpMethod))")
  41. }
  42. done(.success(request))
  43. } catch {
  44. done(.failure(MoyaError.underlying(error, nil)))
  45. }
  46. }
  47. /* 设置ssl
  48. let policies: [String: ServerTrustPolicy] = [
  49. "example.com": .pinPublicKeys(
  50. publicKeys: ServerTrustPolicy.publicKeysInBundle(),
  51. validateCertificateChain: true,
  52. validateHost: true
  53. )
  54. ]
  55. */
  56. // 用Moya默认的Manager还是Alamofire的Manager看实际需求。HTTPS就要手动实现Manager了
  57. //private public func defaultAlamofireManager() -> Manager {
  58. //
  59. // let configuration = URLSessionConfiguration.default
  60. //
  61. // configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
  62. //
  63. // let policies: [String: ServerTrustPolicy] = [
  64. // "ap.grtstar.cn": .disableEvaluation
  65. // ]
  66. // let manager = Alamofire.SessionManager(configuration: configuration,serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies))
  67. //
  68. // manager.startRequestsImmediately = false
  69. //
  70. // return manager
  71. //}
  72. /// NetworkActivityPlugin插件用来监听网络请求
  73. private let networkPlugin = NetworkActivityPlugin.init { (changeType, targetType) in
  74. log.info("networkPlugin \(changeType)")
  75. //targetType 是当前请求的基本信息
  76. switch(changeType){
  77. case .began:
  78. log.info("开始请求网络")
  79. case .ended:
  80. log.info("结束")
  81. }
  82. }
  83. // https://github.com/Moya/Moya/blob/master/docs/Providers.md 参数使用说明
  84. //stubClosure 用来延时发送网络请求
  85. let accountProvider = MoyaProvider<AccountService>(endpointClosure: myEndpointClosure, requestClosure: requestClosure, plugins: [networkPlugin], trackInflights: false)