KMAdsManager.swift 11 KB

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