// // SceneDelegate.swift // Veloria // // Created by Veloria on 2025/5/19. // import UIKit class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? private var onLineTimer: Timer? private(set) var isOpenApp = false func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = (scene as? UIWindowScene) else { return } VPAppTool.sceneDelegate = self VPAppTool.windowScene = windowScene onLineTimer = Timer.scheduledTimer(timeInterval: 60 * 10, target: YYWeakProxy(target: self), selector: #selector(handleOnLine), userInfo: nil, repeats: true) NotificationCenter.default.addObserver(self, selector: #selector(reachabilityDidChangeNotification), name: VPNetworkReachabilityManager.reachabilityDidChangeNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(localizedDidChangeNotification), name: VPLocalizedManager.localizedDidChangeNotification, object: nil) if let webpageURL = connectionOptions.userActivities.first?.webpageURL { self.vp_handleOpenAppMessage(webpageURL: webpageURL) } else if let url = connectionOptions.urlContexts.first?.url { self.vp_handleOpenAppMessage(webpageURL: url) } window = UIWindow(windowScene: windowScene) startApp() } private func setTabBarController() { isOpenApp = true window?.rootViewController = VPTabBarController() window?.makeKeyAndVisible() } func sceneDidDisconnect(_ scene: UIScene) { } func sceneDidBecomeActive(_ scene: UIScene) { vpLog(message: "++++++++++++++sceneDidBecomeActive") enterForeground() } func sceneWillResignActive(_ scene: UIScene) { vpLog(message: "++++++++++++++sceneWillResignActive") enterBackground() } func sceneWillEnterForeground(_ scene: UIScene) { vpLog(message: "++++++++++++++sceneWillEnterForeground") VPAppTool.appDelegate?.uploadNoticeStatus() DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { self.vp_handleOpenAppMessage(webpageURL: SceneDelegate.webpageURL) } } func sceneDidEnterBackground(_ scene: UIScene) { vpLog(message: "++++++++++++++sceneDidEnterBackground") SceneDelegate.allowOpenMessage = true } private func enterForeground() { handleOnLine() VPStatAPI.requestEnterApp() } private func enterBackground() { // if isEnterForeground { // } // vpLog(message: "++++++++++++++enterBackground") // isEnterForeground = false VPStatAPI.requestLeaveApp() } } extension SceneDelegate { private func startApp() { let localizedManager = VPLocalizedManager.shared if localizedManager.localizedDataLocalizedKey != localizedManager.currentLocalizedKey, let lanuchVC = VPAppTool.lanuchViewController { window?.rootViewController = lanuchVC window?.makeKeyAndVisible() self.requestLocalizedData() } else { VPLocalizedManager.shared.updateLocalizedData(completer: nil) setRootVC() } } private func setRootVC() { let hasOpenApp = UserDefaults.standard.object(forKey: kVPHasBeenOpenedAPPDefaultsKey) as? Bool if hasOpenApp != true { ///引导页 let guideVc = VPGuideViewController() guideVc.clickOpenButton = { [weak self] in guard let self = self else { return } self.setTabBarController() self.vp_retryHandleOpenAppMessage() } window?.rootViewController = guideVc window?.makeKeyAndVisible() VPAppTool.appDelegate?.requestAPNS() } else if !self.isOpenApp, hasOpenApp == true, VPNetworkReachabilityManager.manager.isReachable == true, VPLoginManager.manager.userInfo?.user_level == .ad { let vc = VPAppOpenAdViewController() vc.didEndBlock = { [weak self] in self?.setTabBarController() VPAppTool.appDelegate?.requestAPNS() self?.vp_retryHandleOpenAppMessage() } window?.rootViewController = vc window?.makeKeyAndVisible() } else { setTabBarController() VPAppTool.appDelegate?.requestAPNS() } } } extension SceneDelegate { @objc private func handleOnLine() { VPStatAPI.requestStatOnLine() } @objc private func reachabilityDidChangeNotification() { vp_retryHandleOpenAppMessage() if VPNetworkReachabilityManager.manager.isReachable == true { handleOnLine() let data = VPLocalizedManager.shared.localizedData ?? [:] if data.isEmpty { requestLocalizedData() } } } @objc private func localizedDidChangeNotification() { MJRefreshConfig.default.languageCode = VPLocalizedManager.shared.mjLocalizedKey setTabBarController() } private func requestLocalizedData() { VPLocalizedManager.shared.updateLocalizedData { [weak self] finish in guard let self = self else { return } self.setRootVC() } } }