KMFirebaseRemoteConfig.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import Foundation
  2. enum KMRemoteConfigFetchStatus: Int {
  3. case success
  4. case failure
  5. }
  6. let KMFirebaseRemateConfigRequestIsSuccessful = "KMFirebaseRemateConfigRequestIsSuccessful"
  7. let KMFirebaseRemateConfigFinishNoti = "KMFirebaseRemateConfigFinishNoti"
  8. typealias KMRemoteConfigFetchCompletion = (_ status: KMRemoteConfigFetchStatus, _ error: Error?) -> Void
  9. @objcMembers class KMKdanRemoteConfig: NSObject {
  10. private let kIsDisplayAdsKey = "isDisplayAds"
  11. private let kIsDisplayAdsEvaluateAfterKey = "isDisplayAdsEvaluateAfter"
  12. private let kDisplayAdsUrlConfigKey = "displayAdsUrl"
  13. private let kDisplayHouseAdsUrlConfigKey = "displayHouseAdsUrl"
  14. private let kRefreshAdsRateDayKey = "refreshAdsRate"
  15. private let kRefreshAdsRateDayEvaluateAfterKey = "refreshAdsRateEvaluateAfter"
  16. private let kRefreshAdsDateDayKey = "refreshAdsDate"
  17. private let kRefreshAdsDateDayEvaluateAfterKey = "refreshAdsDateEvaluateAfter"
  18. private let kCloseIntervalDateDayKey = "closeIntervalDate"
  19. private let kCloseIntervalDateDayEvaluateAfterKey = "closeIntervalDateEvaluateAfter"
  20. private let kAppEvaluateBeforeAdsCountKey = "appEvaluateBeforeAdsCount"
  21. private let kAppEvaluateAfterAdsCountKey = "appEvaluateAfterAdsCount"
  22. static let remoteConfig = KMKdanRemoteConfig()
  23. private var remoteConfigDatas: [String: Any] = [:]
  24. private var userInfo: [String: Any] = [:]
  25. override init() {
  26. super.init()
  27. let mainBundleString = Bundle.main.bundleIdentifier ?? ""
  28. var dataPath: String?
  29. #if VERSION_DMG
  30. dataPath = Bundle.main.path(forResource: "DMGRemoteConfigDefaults", ofType: "plist")
  31. #else
  32. #if VERSION_FREE
  33. dataPath = Bundle.main.path(forResource: "RemoteConfigDefaults", ofType: "plist")
  34. #else
  35. dataPath = Bundle.main.path(forResource: "ProRemoteConfigDefaults", ofType: "plist")
  36. #endif
  37. #endif
  38. if let path = dataPath, let dict = NSDictionary(contentsOfFile: path) as? [String: Any] {
  39. self.userInfo = dict
  40. }
  41. }
  42. func fetch(completionHandler: @escaping KMRemoteConfigFetchCompletion) {
  43. let mainBundleString = Bundle.main.bundleIdentifier ?? ""
  44. let configuration = URLSessionConfiguration.default
  45. configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
  46. configuration.urlCache = URLCache.shared
  47. let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
  48. let params = "app_code=\(mainBundleString)&app_version=\(appVersion)&key=DisplayAds"
  49. var urlString = "http://store.pdfreaderpro.com:3018/api/get_config"
  50. #if DEBUG
  51. urlString = "http://test-store.kdan.cn:3018/api/get_config"
  52. #endif
  53. if let url = URL(string: "\(urlString)?\(params)") {
  54. var request = URLRequest(url: url)
  55. request.httpMethod = "GET"
  56. request.setValue("application/json", forHTTPHeaderField: "Content-Type")
  57. let session = URLSession(configuration: configuration)
  58. let task = session.dataTask(with: request) { data, response, error in
  59. if let error = error {
  60. completionHandler(.failure, error)
  61. } else if let data = data {
  62. do {
  63. if let datas = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
  64. let displayAds = datas["data"] as? [String: Any] {
  65. #if VERSION_DMG
  66. if let dmg = displayAds["DisplayAds"] as? String {
  67. if let jsonData = dmg.data(using: .utf8),
  68. let dic = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any],
  69. let remoteConfigDatas = dic["dmg"] {
  70. self.remoteConfigDatas = remoteConfigDatas as! [String : Any]
  71. }
  72. }
  73. #else
  74. if let store = displayAds["DisplayAds"] as? String {
  75. if let jsonData = store.data(using: .utf8),
  76. let dic = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any],
  77. let remoteConfigDatas = dic["store"] {
  78. self.remoteConfigDatas = remoteConfigDatas as! [String : Any]
  79. }
  80. }
  81. #endif
  82. }
  83. completionHandler(.success, nil)
  84. } catch {
  85. completionHandler(.failure, error)
  86. }
  87. }
  88. NotificationCenter.default.post(name: Notification.Name(KMFirebaseRemateConfigRequestIsSuccessful), object: self, userInfo: nil)
  89. }
  90. task.resume()
  91. }
  92. }
  93. func isDisplayAds() -> Bool {
  94. var isDisplayAds = userInfo[kIsDisplayAdsKey] as? Bool ?? true
  95. if let value = remoteConfigDatas[kIsDisplayAdsKey] as? Bool {
  96. isDisplayAds = value
  97. }
  98. return isDisplayAds
  99. }
  100. func isDisplayAdsEvaluateAfter() -> Bool {
  101. var isDisplayAds = userInfo[kIsDisplayAdsEvaluateAfterKey] as? Bool ?? true
  102. if let value = remoteConfigDatas[kIsDisplayAdsEvaluateAfterKey] as? Bool {
  103. isDisplayAds = value
  104. }
  105. return isDisplayAds
  106. }
  107. func displayHouseAdsUrl() -> String {
  108. var adsUrl = userInfo[kDisplayHouseAdsUrlConfigKey] as? String ?? ""
  109. if let value = remoteConfigDatas[kDisplayHouseAdsUrlConfigKey] as? String {
  110. adsUrl = value.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
  111. }
  112. return adsUrl
  113. }
  114. func displayAdsUrl() -> String {
  115. var adsUrl = userInfo[kDisplayAdsUrlConfigKey] as? String ?? ""
  116. if let value = remoteConfigDatas[kDisplayAdsUrlConfigKey] as? String {
  117. adsUrl = value.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
  118. }
  119. return adsUrl
  120. }
  121. func refreshAdsRate() -> Int {
  122. var adsRate = userInfo[kRefreshAdsRateDayKey] as? Int ?? 0
  123. if let value = remoteConfigDatas[kRefreshAdsRateDayKey] as? String {
  124. adsRate = Int(value)!
  125. }
  126. return adsRate
  127. }
  128. func refreshAdsRateEvaluateAfter() -> Int {
  129. var adsRate = userInfo[kRefreshAdsRateDayEvaluateAfterKey] as? Int ?? 0
  130. if let value = remoteConfigDatas[kRefreshAdsRateDayEvaluateAfterKey] as? String {
  131. adsRate = Int(value)!
  132. }
  133. return adsRate
  134. }
  135. func refreshAdsDate() -> Int {
  136. var adsDate = userInfo[kRefreshAdsDateDayKey] as? Int ?? 180
  137. if let value = remoteConfigDatas[kRefreshAdsDateDayKey] as? String {
  138. adsDate = Int(value)!
  139. }
  140. return adsDate
  141. }
  142. func refreshAdsDateEvaluateAfter() -> Int {
  143. var adsDate = userInfo[kRefreshAdsDateDayEvaluateAfterKey] as? Int ?? 180
  144. if let value = remoteConfigDatas[kRefreshAdsDateDayEvaluateAfterKey] as? String {
  145. adsDate = Int(value)!
  146. }
  147. return adsDate
  148. }
  149. func closeIntervalDate() -> Int {
  150. var intervalDate = userInfo[kCloseIntervalDateDayKey] as? Int ?? 0
  151. if let value = remoteConfigDatas[kCloseIntervalDateDayKey] as? String {
  152. intervalDate = Int(value)!
  153. }
  154. return intervalDate
  155. }
  156. func closeIntervalDateEvaluateAfter() -> Int {
  157. var intervalDate = userInfo[kCloseIntervalDateDayEvaluateAfterKey] as? Int ?? 0
  158. if let value = remoteConfigDatas[kCloseIntervalDateDayEvaluateAfterKey] as? String {
  159. intervalDate = Int(value)!
  160. }
  161. return intervalDate
  162. }
  163. func appEvaluateBeforeAdsCount() -> Int {
  164. var evaluateCount = userInfo[kAppEvaluateBeforeAdsCountKey] as? Int ?? 1
  165. if let value = remoteConfigDatas[kAppEvaluateBeforeAdsCountKey] as? String {
  166. evaluateCount = Int(value)!
  167. }
  168. return evaluateCount
  169. }
  170. func appEvaluateAfterAdsCount() -> Int {
  171. var evaluateCount = userInfo[kAppEvaluateAfterAdsCountKey] as? Int ?? 1
  172. if let value = remoteConfigDatas[kAppEvaluateAfterAdsCountKey] as? String {
  173. evaluateCount = Int(value)!
  174. }
  175. return evaluateCount
  176. }
  177. func showSDKRecommendInfo() -> Bool {
  178. return false
  179. }
  180. func showAPP_AveragePrice() -> Bool {
  181. return false
  182. }
  183. func showHelp_More_RecommendLink() -> Bool {
  184. return false
  185. }
  186. }