// // AppDelegate.swift // AIEmoji // // Created by 100Years on 2025/1/15. // import UIKit var kAppNewVerison = "" @main class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var backgroundTaskIdentifier: UIBackgroundTaskIdentifier = .invalid func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { window = UIWindow(frame: UIScreen.main.bounds) window?.backgroundColor = UIColor.white window?.makeKeyAndVisible() initPlatform() //首次安装需要等待网络权限弹窗 if AppDelegate.isFirstInstallApp() { goToLoadVC() }else{ PurchaseManager.default.requestProducts() JudgmentSkipPage() } return true } func goToLoadVC() { let launchVC = TSLaunchVC() launchVC.dismissHandler = { [weak self] in guard let self = self else { return } JudgmentSkipPage() } window?.rootViewController = launchVC } func goToTab(){ window?.rootViewController = TSTabBarController() } func JudgmentSkipPage() { if AppDelegate.isFirstInstallApp() { let bootPageVC = TSBootPageVC { [weak self] in guard let self = self else { return } UserDefaults.standard.set("1", forKey: "isFirstInstallApp") UserDefaults.standard.synchronize() goToTab() } let navi = TSBaseNavigationC(rootViewController: bootPageVC) window?.rootViewController = navi }else{ goToTab() } } func initPlatform() { TSColorConfigShared.naviMianTextColor = .white checkAppConfig() } } extension AppDelegate { func applicationWillTerminate(_ application: UIApplication) { // 当应用即将被终止时,这里也可以添加数据保存逻辑,但系统留给的时间很有限 NotificationCenter.default.post(name: .kApplicationWillTerminate, object: nil) } func applicationDidEnterBackground(_ application: UIApplication) { beginBackgroundTask() // ///添加测试数据 // let userDefaults = UserDefaults.standard // let lastGreetingDateString = userDefaults.string(forKey: "kEveryDayPopPurchase") // userDefaults.set(String(Int(lastGreetingDateString!)!+1), forKey: "kEveryDayPopPurchase")//测试用的 } func beginBackgroundTask() { backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask { [weak self] in self?.endBackgroundTask() } } func endBackgroundTask() { if backgroundTaskIdentifier != .invalid { UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) backgroundTaskIdentifier = .invalid } } func applicationWillEnterForeground(_ application: UIApplication) { // handleShowEveryDayPopPurchase() checkAppConfig() } } extension AppDelegate { static func isFirstInstallApp() -> Bool{ return UserDefaults.standard.string(forKey: "isFirstInstallApp") == nil } // func handleShowEveryDayPopPurchase() { // AppDelegate.showEveryDayPopPurchase { vc in // if let vc = vc ,let rootvc = self.window?.rootViewController { // vc.modalPresentationStyle = .fullScreen // rootvc.present(vc, animated: true) // } // } // } // static func showEveryDayPopPurchase(showVCHandle:@escaping (UIViewController?)->Void) { // // if Self.isFirstInstallApp() || kPurchaseDefault.isVip { // //启动页->引导图->会员购买 // showVCHandle(nil) // return // } // // //启动页->会员购买 // // 1. 获取当前日期 // let currentDate = Date() // let dateFormatter = DateFormatter() // dateFormatter.dateFormat = "yyyyMMdd" // let currentDateString = dateFormatter.string(from: currentDate) // // // 2. 获取上次弹窗的日期 // let userDefaults = UserDefaults.standard // let lastGreetingDateString = userDefaults.string(forKey: "kEveryDayPopPurchase") // // // 3. 检查是否需要显示弹窗 "20250319" // if lastGreetingDateString != currentDateString { // // // 4. 弹窗付费引导 // let vc = TSPurchaseVC() // vc.closePageBlock = { // showVCHandle(nil) // } // // showVCHandle(vc) // // 5. 更新上次弹窗的日期 // userDefaults.set(currentDateString, forKey: "kEveryDayPopPurchase") // }else{ // showVCHandle(nil) // } // } static func setsTopDayPopPurchase() { let currentDate = Date() let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyyMMdd" let currentDateString = dateFormatter.string(from: currentDate) UserDefaults.standard.set(currentDateString, forKey: "kEveryDayPopPurchase") } func checkAppConfig(){ _ = TSNetworkShared.get(urlType: .config) { data,error in if let result = kNetWorkResultSuccess(data: data) { kAppNewVerison = result.safeString(forKey: "version") NotificationCenter.default.post(name: .kRefreshSettingView, object: nil) } } } }