AppDelegate.swift 5.1 KB

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