// // SceneDelegate.swift // MoviaBox // // Created by 曾觉新 on 2025/4/8. // import UIKit class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? private var timer: Timer? func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = (scene as? UIWindowScene) else { return } SPAPPTool.sceneDelegate = self SPAPPTool.windowScene = windowScene ///监听语言切换 NotificationCenter.default.addObserver(self, selector: #selector(localizedDidChange), name: SPLocalizedManager.localizedDidChange, object: nil) ///监听网路变化 NotificationCenter.default.addObserver(self, selector: #selector(reachabilityDidChangeNotification), name: SPNetworkReachabilityManager.reachabilityDidChangeNotification, object: nil) ///监听登录状态 // NotificationCenter.default.addObserver(self, selector: #selector(loginStateDidChangeNotification), name: SPLoginManager.loginStateDidChangeNotification, object: nil) if let webpageURL = connectionOptions.userActivities.first?.webpageURL { self.handleOpenAppMessage(webpageURL: webpageURL) } window = UIWindow(windowScene: windowScene) startApp() //在线状态统计 timer = Timer.scheduledTimer(timeInterval: 60 * 10, target: self, selector: #selector(handleOnLine), userInfo: nil, repeats: true) } func sceneDidDisconnect(_ scene: UIScene) { // Called as the scene is being released by the system. // This occurs shortly after the scene enters the background, or when its session is discarded. // Release any resources associated with this scene that can be re-created the next time the scene connects. // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead). } func sceneDidBecomeActive(_ scene: UIScene) { // Called when the scene has moved from an inactive state to an active state. // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. SPStatAPI.requestEnterApp() handleOnLine() } func sceneWillResignActive(_ scene: UIScene) { // Called when the scene will move from an active state to an inactive state. // This may occur due to temporary interruptions (ex. an incoming phone call). SPStatAPI.requestLeaveApp() } func sceneWillEnterForeground(_ scene: UIScene) { // Called as the scene transitions from the background to the foreground. // Use this method to undo the changes made on entering the background. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { self.handleOpenAppMessage(webpageURL: nil) } } func sceneDidEnterBackground(_ scene: UIScene) { // Called as the scene transitions from the foreground to the background. // Use this method to save data, release shared resources, and store enough scene-specific state information // to restore the scene back to its current state. } } extension SceneDelegate { private func startApp() { let localizedManager = SPLocalizedManager.shared ///设置语言与当前的数据语言不同步时,需要重新获取语言后展示页面 if localizedManager.localizedDataLocalizedKey != localizedManager.currentLocalizedKey, let lanuchVC = SPAPPTool.getLanuchViewController() { window?.rootViewController = lanuchVC window?.makeKeyAndVisible() SPLocalizedManager.shared.updateLocalizedData { [weak self] finish in guard let self = self else { return } self.setRootVC() } } else { SPLocalizedManager.shared.updateLocalizedData(completer: nil) setRootVC() } } ///统计在线状态 @objc private func handleOnLine() { SPStatAPI.requestStatOnLine() } private func setRootVC() { let hasOpenApp = UserDefaults.standard.object(forKey: kSPHasBeenOpenedAPPDefaultsKey) as? Bool ///引导页 let guideVc = SPGuideViewController() if hasOpenApp != true && guideVc.lanuchVC != nil { SPAPPTool.isAppOpen = false guideVc.openAppBlock = { self.handleOpenApp() } window?.rootViewController = guideVc window?.makeKeyAndVisible() } else { SPAPPTool.isAppOpen = true setTabBarController() } } private func setTabBarController() { let tabBarController = SPTabBarController() SPAPPTool.mainTabBarController = tabBarController window?.rootViewController = tabBarController window?.makeKeyAndVisible() } ///打开app @objc private func handleOpenApp() { setTabBarController() retryHandleOpenAppMessage() } } extension SceneDelegate { ///语言切换 @objc private func localizedDidChange() { MJRefreshConfig.default.languageCode = SPLocalizedManager.shared.mjLocalizedKey setTabBarController() } ///监听网络变化 @objc private func reachabilityDidChangeNotification() { retryHandleOpenAppMessage() let localizedData = SPLocalizedManager.shared.localizedData ?? [:] if SPNetworkReachabilityManager.manager.isReachable == true { handleOnLine() ///缺少语言数据,重新获取语言数据 if localizedData.isEmpty { self.startApp() } } } }