KMAdsManager.swift 11 KB

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