AppDelegate.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // AppDelegate.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/11/6.
  6. //
  7. import UIKit
  8. import CocoaLumberjackSwift
  9. import Firebase
  10. import GoogleSignIn
  11. @main
  12. class AppDelegate: UIResponder, UIApplicationDelegate {
  13. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  14. // Override point for customization after application launch.
  15. setupLogger()
  16. setupFirebase()
  17. setupAdjust()
  18. LNNetworkMonitor.startMonitoring()
  19. _ = LNProfileManager.shared
  20. _ = LNIMManager.shared
  21. _ = LNGameMateManager.shared
  22. _ = LNPurchaseManager.shared
  23. _ = RechargeManager.shared
  24. _ = LNLocationManager.shared
  25. _ = LNRelationManager.shared
  26. _ = LNDeeplinkManager.shared
  27. _ = LNKeyboardManager.shared
  28. _ = LNConfigManager.shared
  29. _ = LNOrderManager.shared
  30. _ = LNRoomManager.shared
  31. LNEventDeliver.notifyAppLaunchFinished()
  32. if let url = launchOptions?[.url] as? URL {
  33. LNDeeplinkManager.shared.handleDeepLink(url)
  34. }
  35. reportLanguage()
  36. requestNotificationPermissions()
  37. return true
  38. }
  39. // MARK: UISceneSession Lifecycle
  40. func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  41. // Called when a new scene session is being created.
  42. // Use this method to select a configuration to create the new scene with.
  43. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  44. }
  45. func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  46. // Called when the user discards a scene session.
  47. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
  48. // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
  49. }
  50. }
  51. extension AppDelegate {
  52. @objc func businessID() -> Int32 {
  53. LNIMManager.shared.offlinePushAppId
  54. }
  55. @objc func onRemoteNotificationReceived(_ notice: String?) -> Bool {
  56. false
  57. }
  58. }
  59. extension AppDelegate {
  60. private func reportLanguage() {
  61. LNProfileManager.shared.reportCurrentLanguage(code: LNAppConfig.shared.curLang.languageCode) { _ in }
  62. }
  63. }
  64. extension AppDelegate {
  65. private func setupFirebase() {
  66. #if DEBUG
  67. let plistName = "GoogleService-Info-Debug"
  68. #else
  69. let plistName = "GoogleService-Info-Release"
  70. #endif
  71. let plistPath = Bundle.main.path(forResource: plistName, ofType: "plist")!
  72. let options = FirebaseOptions(contentsOfFile: plistPath)
  73. FirebaseApp.configure(options: options!)
  74. }
  75. private func setupLogger() {
  76. #if DEBUG // 只在 Debug 模式打印到终端
  77. let logger = DDOSLogger.sharedInstance
  78. logger.logFormatter = LNLoggerFormater()
  79. DDLog.add(logger)
  80. #endif
  81. let fileLogger = DDFileLogger()
  82. fileLogger.logFormatter = LNLoggerFormater()
  83. fileLogger.rollingFrequency = 24 * 60 * 60 // 1 天轮转
  84. fileLogger.logFileManager.maximumNumberOfLogFiles = 7 // 最多保存 7 个文件
  85. fileLogger.maximumFileSize = 5 * 1024 * 1024 // 5M 最大限制
  86. DDLog.add(fileLogger)
  87. Log.i("\n\n\n\n==================== App Start ====================")
  88. Log.i("version: \(curAppVersion)")
  89. Log.i("buildVersion: \(curBuildVersion)")
  90. Log.i("OS: \(curSystemVersion)")
  91. Log.i("Env: \(LNAppConfig.shared.curEnv.text)")
  92. Log.i("==================== \(curTime.formattedFullDateWithTime()) ====================")
  93. }
  94. private func setupAdjust() {
  95. let token = "fbze46mdkxds"
  96. let env: String
  97. let logLevel: ADJLogLevel
  98. if LNAppConfig.shared.curEnv == .test {
  99. env = ADJEnvironmentSandbox
  100. logLevel = .verbose
  101. } else {
  102. env = ADJEnvironmentProduction
  103. logLevel = .suppress
  104. }
  105. let config = ADJConfig(appToken: token, environment: env)
  106. config?.logLevel = logLevel
  107. config?.externalDeviceId = curDeviceId
  108. config?.enableCostDataInAttribution()
  109. // config?.delegate = self
  110. Adjust.initSdk(config)
  111. }
  112. private func requestNotificationPermissions() {
  113. // 请求通知权限(可根据需求调整,如添加 sound/badge 等)
  114. LNPermissionHelper.requestNotificationAuthorization(options: [.alert, .sound, .badge]) { granted, error in
  115. if let error = error {
  116. Log.e("请求推送权限失败: \(error.localizedDescription)")
  117. return
  118. }
  119. if granted {
  120. Log.i("用户已授予推送权限")
  121. // 权限获取成功后,注册远程推送
  122. UIApplication.shared.registerForRemoteNotifications()
  123. } else {
  124. Log.i("用户拒绝了推送权限")
  125. }
  126. }
  127. }
  128. }