AppDelegate.swift 5.2 KB

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