KMAdsManager.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import Cocoa
  2. import SystemConfiguration
  3. enum KMAdsState: Int {
  4. case none = 0
  5. case initialize
  6. case requestOk
  7. case requestFail
  8. case show
  9. case finish
  10. }
  11. //最后加载完广告的日期
  12. let kLastLoadingDate = "LastLoadingDate"
  13. //今天加载的频次
  14. let kTodayLoadingNumber = "TodayLoadingNumber"
  15. //评价后最后加载完广告的日期
  16. let kLastLoadingDateAfter = "LastLoadingDateAfter"
  17. //评价后今天加载的频次
  18. let kTodayLoadingNumberAfter = "TodayLoadingNumberAfter"
  19. let kADViewWithTag = 1000
  20. class KMAdsManager: NSObject {
  21. private var adViews: [KMAdsWebView] = []
  22. private var isEnableShowAds: Bool = true
  23. private var appFreeTimeAdsCount: Int = 0
  24. private var adsState: KMAdsState = .none
  25. private var appFreeAfterTimeAdsCount: Int = 0
  26. private var timer: Timer?
  27. static let defaultManager = KMAdsManager()
  28. var didClosed = false
  29. private override init() {
  30. super.init()
  31. adViews = []
  32. isEnableShowAds = true
  33. appFreeTimeAdsCount = UserDefaults.standard.integer(forKey: "KMEvaluateBeforeCloseAdsNumbersKey")
  34. appFreeAfterTimeAdsCount = UserDefaults.standard.integer(forKey: "KMEvaluateAfterCloseAdsNumbersKey")
  35. adsState = .none
  36. timer = Timer.scheduledTimer(timeInterval: TimeInterval(8), target: self, selector: #selector(adsWebView_Switch), userInfo: nil, repeats: true)
  37. RunLoop.current.add(timer!, forMode: .common)
  38. }
  39. func beginSheetModal(for view: NSView, directions: KMADViewDirections, animated: Bool, completionHandler handler: @escaping (Int) -> Void) -> Bool {
  40. return beginSheetModalForView(view, directions: directions, adPosY: 30.0, animated: animated, completionHandler: handler)
  41. }
  42. func beginSheetModalForView(_ view: NSView, directions: KMADViewDirections, adPosY posY: CGFloat, animated: Bool, completionHandler handler: @escaping (Int) -> Void) -> Bool {
  43. if let adView = self.adView(at: view) {
  44. adView.removeFromSuperview()
  45. adView.adPosY = posY
  46. view.addSubview(adView)
  47. if adView.adsInfo == nil {
  48. self.refreshDataInfo(with: adView)
  49. } else {
  50. adView.reloadData()
  51. }
  52. return false
  53. }
  54. if self.didClosed {
  55. return false
  56. }
  57. switch adsState {
  58. case .none, .finish:
  59. adsState = .show
  60. default:
  61. break
  62. }
  63. let adView = KMAdsWebView()
  64. adView.adPosY = posY
  65. adView.adDelegate = self
  66. adView.beginSheetModalForView(view: view, directions: directions, animated: animated, completionHandler: handler)
  67. adViews.append(adView)
  68. self.refreshDataInfo(with: adView)
  69. return true
  70. }
  71. func invalidTimer() {
  72. if timer != nil {
  73. timer?.invalidate()
  74. timer = nil
  75. }
  76. }
  77. func refreshDataInfo(with adView: KMAdsWebView) {
  78. if KMAdsInfoManager.shareInstance.adsInfoArrM != nil {
  79. if KMAdsInfoManager.shareInstance.adsInfoArrM.count > 0 {
  80. let adsInfo = KMAdsInfoManager.shareInstance.getRandomAdsInfo()
  81. if adsInfo?.show == true {
  82. adView.adsInfo = adsInfo
  83. adView.reloadData()
  84. } else {
  85. adView.isHidden = true
  86. }
  87. } else {
  88. adView.isHidden = true
  89. }
  90. }
  91. }
  92. func adView(at view: NSView) -> KMAdsWebView? {
  93. guard let adView = adViews.first else {
  94. return nil
  95. }
  96. return adView
  97. }
  98. func dismissSheetModal(for view: NSView) {
  99. if let adView = adViews.first {
  100. adViews.removeAll { $0 == adView }
  101. adView.removeFromSuperview()
  102. }
  103. adsState = .finish
  104. }
  105. func showFreeTimeAdsInfo() {
  106. print("KMPurchaseCompareWindowController")
  107. }
  108. func checkNetworkAvailable() -> Bool {
  109. guard let reachability = SCNetworkReachabilityCreateWithName(nil, "www.apple.com") else {
  110. return false
  111. }
  112. var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
  113. if SCNetworkReachabilityGetFlags(reachability, &flags) {
  114. let isDataSourceAvailable = flags.contains(.reachable) && !flags.contains(.connectionRequired)
  115. return isDataSourceAvailable
  116. }
  117. return false
  118. }
  119. func refreshLoadingDate() {
  120. var todayLoadingNumber = 0
  121. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  122. todayLoadingNumber = UserDefaults.standard.integer(forKey: kTodayLoadingNumber)
  123. todayLoadingNumber += 1
  124. UserDefaults.standard.set(todayLoadingNumber, forKey: kTodayLoadingNumber)
  125. let date = Date()
  126. let formatter = DateFormatter()
  127. formatter.dateFormat = "yyyy-MM-dd"
  128. let str = formatter.string(from: date)
  129. UserDefaults.standard.set(str, forKey: kLastLoadingDate)
  130. } else {
  131. todayLoadingNumber = UserDefaults.standard.integer(forKey: kTodayLoadingNumberAfter)
  132. todayLoadingNumber += 1
  133. UserDefaults.standard.set(todayLoadingNumber, forKey: kTodayLoadingNumberAfter)
  134. let date = Date()
  135. let formatter = DateFormatter()
  136. formatter.dateFormat = "yyyy-MM-dd"
  137. let str = formatter.string(from: date)
  138. UserDefaults.standard.set(str, forKey: kLastLoadingDateAfter)
  139. }
  140. }
  141. func checkTheDate() -> Bool {
  142. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  143. let dateToday = Date()
  144. let formatter = DateFormatter()
  145. formatter.dateFormat = "yyyy-MM-dd"
  146. let str = formatter.string(from: dateToday)
  147. let string = UserDefaults.standard.string(forKey: kLastLoadingDate) ?? "1990-01-01"
  148. let dateFormatter = DateFormatter()
  149. dateFormatter.dateFormat = "yyyy-MM-dd"
  150. guard let lastDate = dateFormatter.date(from: string) else {
  151. return false
  152. }
  153. let isToday = Calendar.current.isDateInToday(lastDate)
  154. if !isToday {
  155. UserDefaults.standard.set(0, forKey: kTodayLoadingNumber)
  156. return true
  157. } else {
  158. var todayLoadingNumber = 0
  159. if UserDefaults.standard.integer(forKey: kTodayLoadingNumber) != 0 {
  160. todayLoadingNumber = UserDefaults.standard.integer(forKey: kTodayLoadingNumber)
  161. }
  162. if todayLoadingNumber >= KMKdanRemoteConfig.remoteConfig.refreshAdsRate() {
  163. return false
  164. }
  165. }
  166. } else {
  167. let dateToday = Date()
  168. let formatter = DateFormatter()
  169. formatter.dateFormat = "yyyy-MM-dd"
  170. let str = formatter.string(from: dateToday)
  171. let string = UserDefaults.standard.string(forKey: kLastLoadingDateAfter) ?? str
  172. let dateFormatter = DateFormatter()
  173. dateFormatter.dateFormat = "yyyy-MM-dd"
  174. guard let lastDate = dateFormatter.date(from: string) else {
  175. return false
  176. }
  177. let isToday = Calendar.current.isDateInToday(lastDate)
  178. if !isToday {
  179. UserDefaults.standard.set(0, forKey: kTodayLoadingNumberAfter)
  180. return true
  181. } else {
  182. var todayLoadingNumber = 0
  183. if UserDefaults.standard.integer(forKey: kTodayLoadingNumberAfter) != 0 {
  184. todayLoadingNumber = UserDefaults.standard.integer(forKey: kTodayLoadingNumberAfter)
  185. }
  186. if todayLoadingNumber >= KMKdanRemoteConfig.remoteConfig.refreshAdsRateEvaluateAfter() {
  187. return false
  188. }
  189. }
  190. }
  191. return true
  192. }
  193. func updateShowAdsDate() {
  194. let defaults = UserDefaults.standard
  195. let format = DateFormatter()
  196. format.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
  197. let currentDate = Date()
  198. let strCurDate = format.string(from: currentDate)
  199. defaults.setValue(strCurDate, forKey: "km_app_show_ads_date")
  200. defaults.synchronize()
  201. }
  202. func isValidLastShowAds() -> Bool {
  203. let defaults = UserDefaults.standard
  204. var tAdsFlag = false
  205. if let startStr = defaults.string(forKey: "km_app_show_ads_date") {
  206. let format = DateFormatter()
  207. format.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
  208. guard let startD = format.date(from: startStr) else {
  209. return false
  210. }
  211. var closeDate = 0
  212. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  213. closeDate = KMKdanRemoteConfig.remoteConfig.closeIntervalDate()
  214. } else {
  215. closeDate = KMKdanRemoteConfig.remoteConfig.closeIntervalDateEvaluateAfter()
  216. }
  217. let tCurDate = Date(timeIntervalSinceNow: TimeInterval(-closeDate))
  218. let result = tCurDate.compare(startD)
  219. if result == .orderedDescending {
  220. tAdsFlag = true
  221. }
  222. } else {
  223. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  224. tAdsFlag = KMKdanRemoteConfig.remoteConfig.isDisplayAds()
  225. } else {
  226. tAdsFlag = KMKdanRemoteConfig.remoteConfig.isDisplayAdsEvaluateAfter()
  227. }
  228. }
  229. return tAdsFlag
  230. }
  231. @objc func adsWebView_Switch() {
  232. DispatchQueue.main.async {
  233. if self.adViews.count > 1 {
  234. if let adView = self.adViews.first {
  235. self.refreshDataInfo(with: adView)
  236. }
  237. } else {
  238. self.invalidTimer()
  239. }
  240. }
  241. }
  242. }
  243. extension KMAdsManager: AdsWebViewDelegate {
  244. // MARK: - KMADViewDelegate
  245. func kmAdViewClose(_ tView: KMAdsWebView) {
  246. for view in self.adViews {
  247. view.removeFromSuperview()
  248. }
  249. self.adViews.removeAll()
  250. self.adsState = .finish
  251. self.updateShowAdsDate()
  252. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  253. self.appFreeTimeAdsCount = (self.appFreeTimeAdsCount + 1) % KMKdanRemoteConfig.remoteConfig.appEvaluateBeforeAdsCount()
  254. UserDefaults.standard.set(self.appFreeTimeAdsCount, forKey: "KMEvaluateBeforeCloseAdsNumbersKey")
  255. if 0 == (self.appFreeTimeAdsCount % KMKdanRemoteConfig.remoteConfig.appEvaluateBeforeAdsCount()) {
  256. self.showFreeTimeAdsInfo()
  257. }
  258. } else {
  259. self.appFreeAfterTimeAdsCount = (self.appFreeAfterTimeAdsCount + 1) % KMKdanRemoteConfig.remoteConfig.appEvaluateAfterAdsCount()
  260. UserDefaults.standard.set(self.appFreeAfterTimeAdsCount, forKey: "KMEvaluateAfterCloseAdsNumbersKey")
  261. if 0 == (self.appFreeAfterTimeAdsCount % KMKdanRemoteConfig.remoteConfig.appEvaluateAfterAdsCount()) {
  262. self.showFreeTimeAdsInfo()
  263. }
  264. }
  265. self.didClosed = true
  266. self.invalidTimer()
  267. }
  268. func kmAdViewClicked(_ tView: KMAdsWebView) {
  269. for view in self.adViews {
  270. view.removeFromSuperview()
  271. }
  272. self.adViews.removeAll()
  273. self.adsState = .finish
  274. self.updateShowAdsDate()
  275. self.didClosed = true
  276. self.invalidTimer()
  277. }
  278. }