AppDelegate.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // AppDelegate.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/1/15.
  6. //
  7. import UIKit
  8. var kAppNewVerison = ""
  9. @main
  10. class AppDelegate: UIResponder, UIApplicationDelegate {
  11. var window: UIWindow?
  12. var backgroundTaskIdentifier: UIBackgroundTaskIdentifier = .invalid
  13. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  14. window = UIWindow(frame: UIScreen.main.bounds)
  15. window?.backgroundColor = UIColor.white
  16. window?.makeKeyAndVisible()
  17. initPlatform()
  18. //首次安装需要等待网络权限弹窗
  19. if AppDelegate.isFirstInstallApp() {
  20. goToLoadVC()
  21. }else{
  22. PurchaseManager.default.requestProducts()
  23. JudgmentSkipPage()
  24. }
  25. return true
  26. }
  27. func goToLoadVC() {
  28. let launchVC = TSLaunchVC()
  29. launchVC.dismissHandler = { [weak self] in
  30. guard let self = self else { return }
  31. JudgmentSkipPage()
  32. }
  33. window?.rootViewController = launchVC
  34. }
  35. func goToTab(){
  36. window?.rootViewController = TSTabBarController()
  37. }
  38. func JudgmentSkipPage() {
  39. if AppDelegate.isFirstInstallApp() {
  40. let bootPageVC = TSBootPageVC { [weak self] in
  41. guard let self = self else { return }
  42. UserDefaults.standard.set("1", forKey: "isFirstInstallApp")
  43. UserDefaults.standard.synchronize()
  44. goToTab()
  45. }
  46. let navi = TSBaseNavigationC(rootViewController: bootPageVC)
  47. window?.rootViewController = navi
  48. }else{
  49. goToTab()
  50. }
  51. }
  52. func initPlatform() {
  53. TSColorConfigShared.naviMianTextColor = .white
  54. checkAppConfig()
  55. }
  56. }
  57. extension AppDelegate {
  58. func applicationWillTerminate(_ application: UIApplication) {
  59. // 当应用即将被终止时,这里也可以添加数据保存逻辑,但系统留给的时间很有限
  60. NotificationCenter.default.post(name: .kApplicationWillTerminate, object: nil)
  61. }
  62. func applicationDidEnterBackground(_ application: UIApplication) {
  63. beginBackgroundTask()
  64. // ///添加测试数据
  65. // let userDefaults = UserDefaults.standard
  66. // let lastGreetingDateString = userDefaults.string(forKey: "kEveryDayPopPurchase")
  67. // userDefaults.set(String(Int(lastGreetingDateString!)!+1), forKey: "kEveryDayPopPurchase")//测试用的
  68. }
  69. func beginBackgroundTask() {
  70. backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask { [weak self] in
  71. self?.endBackgroundTask()
  72. }
  73. }
  74. func endBackgroundTask() {
  75. if backgroundTaskIdentifier != .invalid {
  76. UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier)
  77. backgroundTaskIdentifier = .invalid
  78. }
  79. }
  80. func applicationWillEnterForeground(_ application: UIApplication) {
  81. // handleShowEveryDayPopPurchase()
  82. checkAppConfig()
  83. }
  84. }
  85. extension AppDelegate {
  86. static func isFirstInstallApp() -> Bool{
  87. return UserDefaults.standard.string(forKey: "isFirstInstallApp") == nil
  88. }
  89. // func handleShowEveryDayPopPurchase() {
  90. // AppDelegate.showEveryDayPopPurchase { vc in
  91. // if let vc = vc ,let rootvc = self.window?.rootViewController {
  92. // vc.modalPresentationStyle = .fullScreen
  93. // rootvc.present(vc, animated: true)
  94. // }
  95. // }
  96. // }
  97. // static func showEveryDayPopPurchase(showVCHandle:@escaping (UIViewController?)->Void) {
  98. //
  99. // if Self.isFirstInstallApp() || kPurchaseDefault.isVip {
  100. // //启动页->引导图->会员购买
  101. // showVCHandle(nil)
  102. // return
  103. // }
  104. //
  105. // //启动页->会员购买
  106. // // 1. 获取当前日期
  107. // let currentDate = Date()
  108. // let dateFormatter = DateFormatter()
  109. // dateFormatter.dateFormat = "yyyyMMdd"
  110. // let currentDateString = dateFormatter.string(from: currentDate)
  111. //
  112. // // 2. 获取上次弹窗的日期
  113. // let userDefaults = UserDefaults.standard
  114. // let lastGreetingDateString = userDefaults.string(forKey: "kEveryDayPopPurchase")
  115. //
  116. // // 3. 检查是否需要显示弹窗 "20250319"
  117. // if lastGreetingDateString != currentDateString {
  118. //
  119. // // 4. 弹窗付费引导
  120. // let vc = TSPurchaseVC()
  121. // vc.closePageBlock = {
  122. // showVCHandle(nil)
  123. // }
  124. //
  125. // showVCHandle(vc)
  126. // // 5. 更新上次弹窗的日期
  127. // userDefaults.set(currentDateString, forKey: "kEveryDayPopPurchase")
  128. // }else{
  129. // showVCHandle(nil)
  130. // }
  131. // }
  132. static func setsTopDayPopPurchase() {
  133. let currentDate = Date()
  134. let dateFormatter = DateFormatter()
  135. dateFormatter.dateFormat = "yyyyMMdd"
  136. let currentDateString = dateFormatter.string(from: currentDate)
  137. UserDefaults.standard.set(currentDateString, forKey: "kEveryDayPopPurchase")
  138. }
  139. func checkAppConfig(){
  140. _ = TSNetworkShared.get(urlType: .config) { data,error in
  141. if let result = kNetWorkResultSuccess(data: data) {
  142. kAppNewVerison = result.safeString(forKey: "version")
  143. NotificationCenter.default.post(name: .kRefreshSettingView, object: nil)
  144. }
  145. }
  146. }
  147. }