Fableon/Fableon/App/SceneDelegate.swift
湖北秦九 e0163dabe3 1
2025-10-31 13:36:20 +08:00

80 lines
2.3 KiB
Swift

//
// SceneDelegate.swift
// Fableon
//
// Created by on 2025/9/15.
//
import UIKit
import YYText
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
private var onLineTimer: Timer?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
NotificationCenter.default.addObserver(self, selector: #selector(networkStatusDidChangeNotification), name: FANetworkMonitor.networkStatusDidChangeNotification, object: nil)
FATool.sceneDelegate = self
FATool.windowScene = windowScene
window = UIWindow(windowScene: windowScene)
window?.rootViewController = FATabBarController()
window?.makeKeyAndVisible()
FAAdjustStateManager.manager.isOpenApp = true
onLineTimer = Timer.scheduledTimer(timeInterval: 60 * 10, target: YYTextWeakProxy(target: self), selector: #selector(handleOnLine), userInfo: nil, repeats: true)
if let webpageURL = connectionOptions.userActivities.first?.webpageURL {
self.handleOpenAppMessage(webpageURL: webpageURL)
} else if let url = connectionOptions.urlContexts.first?.url {
self.handleOpenAppMessage(webpageURL: url)
}
requestFirebase()
requestApns()
}
func sceneDidDisconnect(_ scene: UIScene) {
}
func sceneDidBecomeActive(_ scene: UIScene) {
self.handleOnLine()
FAStatAPI.requestEnterApp()
self.setBadgeCount(0)
}
func sceneWillResignActive(_ scene: UIScene) {
FAStatAPI.requestLeaveApp()
}
func sceneWillEnterForeground(_ scene: UIScene) {
FAStatAPI.uploadApnsAuthorizationStatus()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.handleOpenAppMessage(webpageURL: nil)
}
}
func sceneDidEnterBackground(_ scene: UIScene) {
FAAdjustStateManager.manager.allowOpenMessage = true
}
}
extension SceneDelegate {
@objc private func handleOnLine() {
FAStatAPI.requestStatOnLine()
}
@objc private func networkStatusDidChangeNotification() {
FATool.requestIDFAAuthorization(nil)
self.retryHandleOpenAppMessage()
}
}