KMAdsManager.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. // KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  114. }
  115. func checkNetworkAvailable() -> Bool {
  116. guard let reachability = SCNetworkReachabilityCreateWithName(nil, "www.apple.com") else {
  117. return false
  118. }
  119. var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
  120. if SCNetworkReachabilityGetFlags(reachability, &flags) {
  121. let isDataSourceAvailable = flags.contains(.reachable) && !flags.contains(.connectionRequired)
  122. return isDataSourceAvailable
  123. }
  124. return false
  125. }
  126. func refreshLoadingDate() {
  127. var todayLoadingNumber = 0
  128. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  129. todayLoadingNumber = UserDefaults.standard.integer(forKey: kTodayLoadingNumber)
  130. todayLoadingNumber += 1
  131. UserDefaults.standard.set(todayLoadingNumber, forKey: kTodayLoadingNumber)
  132. let date = Date()
  133. let formatter = DateFormatter()
  134. formatter.dateFormat = "yyyy-MM-dd"
  135. let str = formatter.string(from: date)
  136. UserDefaults.standard.set(str, forKey: kLastLoadingDate)
  137. } else {
  138. todayLoadingNumber = UserDefaults.standard.integer(forKey: kTodayLoadingNumberAfter)
  139. todayLoadingNumber += 1
  140. UserDefaults.standard.set(todayLoadingNumber, forKey: kTodayLoadingNumberAfter)
  141. let date = Date()
  142. let formatter = DateFormatter()
  143. formatter.dateFormat = "yyyy-MM-dd"
  144. let str = formatter.string(from: date)
  145. UserDefaults.standard.set(str, forKey: kLastLoadingDateAfter)
  146. }
  147. }
  148. func checkTheDate() -> Bool {
  149. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  150. let dateToday = Date()
  151. let formatter = DateFormatter()
  152. formatter.dateFormat = "yyyy-MM-dd"
  153. let str = formatter.string(from: dateToday)
  154. let string = UserDefaults.standard.string(forKey: kLastLoadingDate) ?? "1990-01-01"
  155. let dateFormatter = DateFormatter()
  156. dateFormatter.dateFormat = "yyyy-MM-dd"
  157. guard let lastDate = dateFormatter.date(from: string) else {
  158. return false
  159. }
  160. let isToday = Calendar.current.isDateInToday(lastDate)
  161. if !isToday {
  162. UserDefaults.standard.set(0, forKey: kTodayLoadingNumber)
  163. return true
  164. } else {
  165. var todayLoadingNumber = 0
  166. if UserDefaults.standard.integer(forKey: kTodayLoadingNumber) != 0 {
  167. todayLoadingNumber = UserDefaults.standard.integer(forKey: kTodayLoadingNumber)
  168. }
  169. if todayLoadingNumber >= KMKdanRemoteConfig.remoteConfig.refreshAdsRate() {
  170. return false
  171. }
  172. }
  173. } else {
  174. let dateToday = Date()
  175. let formatter = DateFormatter()
  176. formatter.dateFormat = "yyyy-MM-dd"
  177. let str = formatter.string(from: dateToday)
  178. let string = UserDefaults.standard.string(forKey: kLastLoadingDateAfter) ?? str
  179. let dateFormatter = DateFormatter()
  180. dateFormatter.dateFormat = "yyyy-MM-dd"
  181. guard let lastDate = dateFormatter.date(from: string) else {
  182. return false
  183. }
  184. let isToday = Calendar.current.isDateInToday(lastDate)
  185. if !isToday {
  186. UserDefaults.standard.set(0, forKey: kTodayLoadingNumberAfter)
  187. return true
  188. } else {
  189. var todayLoadingNumber = 0
  190. if UserDefaults.standard.integer(forKey: kTodayLoadingNumberAfter) != 0 {
  191. todayLoadingNumber = UserDefaults.standard.integer(forKey: kTodayLoadingNumberAfter)
  192. }
  193. if todayLoadingNumber >= KMKdanRemoteConfig.remoteConfig.refreshAdsRateEvaluateAfter() {
  194. return false
  195. }
  196. }
  197. }
  198. return true
  199. }
  200. func updateShowAdsDate() {
  201. let defaults = UserDefaults.standard
  202. let format = DateFormatter()
  203. format.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
  204. let currentDate = Date()
  205. let strCurDate = format.string(from: currentDate)
  206. defaults.setValue(strCurDate, forKey: "km_app_show_ads_date")
  207. defaults.synchronize()
  208. }
  209. func isValidLastShowAds() -> Bool {
  210. let defaults = UserDefaults.standard
  211. var tAdsFlag = false
  212. if let startStr = defaults.string(forKey: "km_app_show_ads_date") {
  213. let format = DateFormatter()
  214. format.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
  215. guard let startD = format.date(from: startStr) else {
  216. return false
  217. }
  218. var closeDate = 0
  219. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  220. closeDate = KMKdanRemoteConfig.remoteConfig.closeIntervalDate()
  221. } else {
  222. closeDate = KMKdanRemoteConfig.remoteConfig.closeIntervalDateEvaluateAfter()
  223. }
  224. let tCurDate = Date(timeIntervalSinceNow: TimeInterval(-closeDate))
  225. let result = tCurDate.compare(startD)
  226. if result == .orderedDescending {
  227. tAdsFlag = true
  228. }
  229. } else {
  230. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  231. tAdsFlag = KMKdanRemoteConfig.remoteConfig.isDisplayAds()
  232. } else {
  233. tAdsFlag = KMKdanRemoteConfig.remoteConfig.isDisplayAdsEvaluateAfter()
  234. }
  235. }
  236. return tAdsFlag
  237. }
  238. }
  239. extension KMAdsManager: AdsWebViewDelegate {
  240. // MARK: - KMADViewDelegate
  241. func kmAdViewClose(_ tView: KMAdsWebView) {
  242. for view in self.adViews {
  243. view.stopLoading()
  244. view.removeFromSuperview()
  245. }
  246. self.adViews.removeAll()
  247. self.adsState = .finish
  248. self.updateShowAdsDate()
  249. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  250. self.appFreeTimeAdsCount = (self.appFreeTimeAdsCount + 1) % KMKdanRemoteConfig.remoteConfig.appEvaluateBeforeAdsCount()
  251. UserDefaults.standard.set(self.appFreeTimeAdsCount, forKey: "KMEvaluateBeforeCloseAdsNumbersKey")
  252. if 0 == (self.appFreeTimeAdsCount % KMKdanRemoteConfig.remoteConfig.appEvaluateBeforeAdsCount()) {
  253. self.showFreeTimeAdsInfo()
  254. }
  255. } else {
  256. self.appFreeAfterTimeAdsCount = (self.appFreeAfterTimeAdsCount + 1) % KMKdanRemoteConfig.remoteConfig.appEvaluateAfterAdsCount()
  257. UserDefaults.standard.set(self.appFreeAfterTimeAdsCount, forKey: "KMEvaluateAfterCloseAdsNumbersKey")
  258. if 0 == (self.appFreeAfterTimeAdsCount % KMKdanRemoteConfig.remoteConfig.appEvaluateAfterAdsCount()) {
  259. self.showFreeTimeAdsInfo()
  260. }
  261. }
  262. }
  263. func kmAdViewClicked(_ tView: KMAdsWebView) {
  264. for view in self.adViews {
  265. view.stopLoading()
  266. view.removeFromSuperview()
  267. }
  268. self.adViews.removeAll()
  269. self.adsState = .finish
  270. self.updateShowAdsDate()
  271. }
  272. }