TSLaunchVC.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // TSLaunchVC.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2024/12/20.
  6. //
  7. import Alamofire
  8. import UIKit
  9. class TSLaunchVC: UIViewController {
  10. var dismissHandler: (() -> Void)?
  11. // 定时器
  12. private var timer: DispatchSourceTimer?
  13. // 闪屏页剩余显示时长
  14. #if DEBUG
  15. private var remindTimeInterval: TimeInterval = 1.5
  16. #else
  17. private var remindTimeInterval: TimeInterval = 3.0
  18. #endif
  19. private var isStop: Bool = false
  20. var showingAdViewController: Bool {
  21. return presentedViewController != nil
  22. }
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. setupLaunchScreenView()
  26. startTimer()
  27. addNotifiy()
  28. }
  29. func addNotifiy() {
  30. TSNetworkShared.startListenNetStatus { status, manager in
  31. switch status {
  32. case .reachable:
  33. kPurchaseToolShared.luanchuPrchase()
  34. // AppDelegate.requestAdTrack()
  35. manager?.stopListening()
  36. // self.initAdMob()
  37. break
  38. default:
  39. // AppDelegate.requestAdTrack()
  40. kPurchaseToolShared.luanchuPrchase()
  41. // self.initAdMob()
  42. break
  43. }
  44. }
  45. }
  46. func initAdMob() {
  47. // GADMobileAds.sharedInstance().start { status in
  48. // print("启动状态 == status === \(status.adapterStatusesByClassName)")
  49. // }
  50. }
  51. func enterApp() {
  52. showEveryDayPopPurchase()
  53. // DispatchQueue.main.async {
  54. // self.dismissHandler?()
  55. // }
  56. }
  57. private func startTimer() {
  58. if timer == nil {
  59. timer = DispatchSource.makeTimerSource(queue: DispatchQueue.global())
  60. timer?.schedule(deadline: .now(), repeating: .milliseconds(200))
  61. timer?.setEventHandler(handler: { [weak self] in
  62. // App活跃时,计时有效;如网络授权弹窗唤起时,计时失效
  63. DispatchQueue.main.async {
  64. guard let self = self,
  65. UIApplication.shared.applicationState == .active,
  66. !self.showingAdViewController else {
  67. return
  68. }
  69. self.remindTimeInterval -= 0.2
  70. print("倒计时:\(self.remindTimeInterval)")
  71. if self.remindTimeInterval <= 0 {
  72. self.timer?.cancel()
  73. self.enterApp()
  74. }
  75. }
  76. })
  77. timer?.resume()
  78. }
  79. }
  80. private func setupLaunchScreenView() {
  81. // 获取 LaunchScreen.storyboard 的视图控制器
  82. let storyboard = UIStoryboard(name: "LaunchScreen", bundle: nil)
  83. guard let launchVC = storyboard.instantiateInitialViewController() else {
  84. debugPrint("无法加载 LaunchScreen 的初始视图控制器")
  85. return
  86. }
  87. // 获取 LaunchScreen 的视图
  88. guard let launchView = launchVC.view else {
  89. debugPrint("无法获取 LaunchScreen 的视图")
  90. return
  91. }
  92. // 设置 launchView 的 frame 为当前视图的 bounds
  93. launchView.frame = UIScreen.main.bounds
  94. // 将 launchView 添加到 TSLaunchVC 的视图中
  95. view.addSubview(launchView)
  96. }
  97. }
  98. extension TSLaunchVC {
  99. func showEveryDayPopPurchase() {
  100. if UserDefaults.standard.string(forKey: "isFirstInstallApp") == nil || kPurchaseToolShared.isVip {
  101. //启动页->引导图->会员购买
  102. dismissHandler?()
  103. return
  104. }
  105. //启动页->会员购买
  106. // 1. 获取当前日期
  107. let currentDate = Date()
  108. let dateFormatter = DateFormatter()
  109. dateFormatter.dateFormat = "yyyyMMdd"
  110. let currentDateString = dateFormatter.string(from: currentDate)
  111. // 2. 获取上次弹窗的日期
  112. let userDefaults = UserDefaults.standard
  113. let lastGreetingDateString = userDefaults.string(forKey: "kEveryDayPopPurchase")
  114. // 3. 检查是否需要显示弹窗 "20250319"
  115. if lastGreetingDateString != currentDateString {
  116. // 4. 弹窗付费引导
  117. let vc = TSPurchaseVC()
  118. vc.closePageBlock = { [weak self] in
  119. guard let self = self else { return }
  120. dismissHandler?()
  121. }
  122. self.navigationController?.pushViewController(vc, animated: true)
  123. // 5. 更新上次弹窗的日期
  124. userDefaults.set(currentDateString, forKey: "kEveryDayPopPurchase")
  125. // userDefaults.set(String(Int(currentDateString)!+1), forKey: "kEveryDayPopPurchase")测试用的
  126. }else{
  127. dismissHandler?()
  128. }
  129. }
  130. }