SceneDelegate.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // SceneDelegate.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/11/6.
  6. //
  7. import UIKit
  8. import Combine
  9. #if DEBUG
  10. import DoraemonKit
  11. #endif
  12. class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  13. var window: UIWindow?
  14. func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
  15. handleUniversalLink(userActivity: userActivity)
  16. }
  17. func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  18. // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
  19. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
  20. // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
  21. guard let sceneWindow = (scene as? UIWindowScene) else { return }
  22. window = UIWindow(windowScene: sceneWindow)
  23. window?.backgroundColor = .white
  24. window?.overrideUserInterfaceStyle = .light
  25. window?.makeKeyAndVisible()
  26. window?.rootViewController = LNNavigationController(rootViewController: LNMainViewController())
  27. LNEventDeliver.addObserver(self)
  28. autoLoginIfNeed()
  29. #if DEBUG
  30. DoraemonManager.shareInstance().install()
  31. #endif
  32. if let userActivity = connectionOptions.userActivities.first {
  33. handleUniversalLink(userActivity: userActivity)
  34. }
  35. }
  36. func sceneDidDisconnect(_ scene: UIScene) {
  37. // Called as the scene is being released by the system.
  38. // This occurs shortly after the scene enters the background, or when its session is discarded.
  39. // Release any resources associated with this scene that can be re-created the next time the scene connects.
  40. // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
  41. }
  42. func sceneDidBecomeActive(_ scene: UIScene) {
  43. // Called when the scene has moved from an inactive state to an active state.
  44. // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
  45. }
  46. func sceneWillResignActive(_ scene: UIScene) {
  47. // Called when the scene will move from an active state to an inactive state.
  48. // This may occur due to temporary interruptions (ex. an incoming phone call).
  49. }
  50. func sceneWillEnterForeground(_ scene: UIScene) {
  51. // Called as the scene transitions from the background to the foreground.
  52. // Use this method to undo the changes made on entering the background.
  53. }
  54. func sceneDidEnterBackground(_ scene: UIScene) {
  55. // Called as the scene transitions from the foreground to the background.
  56. // Use this method to save data, release shared resources, and store enough scene-specific state information
  57. // to restore the scene back to its current state.
  58. }
  59. func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
  60. if let urlContext = URLContexts.first {
  61. let url = urlContext.url
  62. LNDeeplinkManager.shared.handleDeepLink(url)
  63. }
  64. }
  65. }
  66. extension SceneDelegate: LNNetworkMonitorNotify {
  67. func onNetworkStateChanged(state: LNNetworkState) {
  68. if state == .available {
  69. autoLoginIfNeed()
  70. } else {
  71. Log.d("network invailable")
  72. }
  73. }
  74. }
  75. extension SceneDelegate: LNAppMainEvent {
  76. func onAppLanguageChanged(newLanguage: LNAppLanguage) {
  77. let nav = LNNavigationController(rootViewController: LNMainViewController())
  78. window?.rootViewController = nav
  79. nav.viewControllers.append(LNSettingsViewController())
  80. LNProfileManager.shared.reportCurrentLanguage(code: newLanguage.languageCode) { _ in }
  81. }
  82. }
  83. extension SceneDelegate: LNAccountManagerNotify {
  84. func onUserLogout() {
  85. window?.rootViewController = LNNavigationController(rootViewController: LNMainViewController())
  86. }
  87. func onUserLogin() {
  88. guard let nav = window?.rootViewController as? UINavigationController else {
  89. return
  90. }
  91. if let index = nav.viewControllers.lastIndex(where: {
  92. !($0 is LNLoginPhoneInputViewController
  93. || $0 is LNLoginCaptchaInputViewController) }) {
  94. let viewControllers = Array(nav.viewControllers[0...index])
  95. nav.setViewControllers(viewControllers, animated: true)
  96. }
  97. }
  98. }
  99. extension SceneDelegate {
  100. private func autoLoginIfNeed() {
  101. guard LNAccountManager.shared.wasLogin,
  102. LNNetworkMonitor.curState == .available else { return }
  103. LNAccountManager.shared.loginByToken()
  104. }
  105. private func handleUniversalLink(userActivity: NSUserActivity) {
  106. // 验证是否为 Universal Link
  107. guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
  108. let url = userActivity.webpageURL else {
  109. return
  110. }
  111. LNDeeplinkManager.shared.handleDeepLink(url)
  112. }
  113. }