// // AIGrammarApp.swift // AIGrammar // // Created by oscar on 2024/3/27. // import SwiftUI import TrustDecision import Combine import Firebase import FirebaseAnalytics import FirebaseCrashlytics @main struct AIGrammarApp: App { // 获取 AppDelegate 的实例 @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate let persistenceController = PersistenceController.shared @StateObject private var appState = NofifyState() @State private var urlToOpen: String? init() { // 将当前实例传递给 AppDelegate appDelegate.app = self // 初始化部分 setupLogging() _ = InitApp.shared // firebase 默认info级别,这里修改成线上级别。 FirebaseConfiguration.shared.setLoggerLevel(.error) FirebaseApp.configure() Crashlytics.crashlytics().setCrashlyticsCollectionEnabled(true) // 测试一下崩溃,需要移除。 // Crashlytics.crashlytics().log("Test crash") // fatalError("Test crash from Crashlytics") } var body: some Scene { WindowGroup { AllTabView(selectedTab: $appState.selectedTab, showPromotion: $appState.showPromotion, promotionMode: $appState.promotionMode) .environment(\.managedObjectContext, persistenceController.container.viewContext) .environmentObject(IAPManager()) // 这里添加 IAPManager .environmentObject(globalEnvironment) // 这里添加 IAPManager .environmentObject(appState) .preferredColorScheme(.light) // 强制整个应用使用亮色模式 .sheet(isPresented: $appState.showPromotion) { switch appState.promotionMode { case .halfScreen: PromotionView() // 半屏模式 .presentationDetents([.medium]) case .fullScreen: PromotionView() // 全屏模式 .edgesIgnoringSafeArea(.all) default: EmptyView() } } .onChange(of: urlToOpen) { url in if let url = url, let link = URL(string: url) { UIApplication.shared.open(link) } } .onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in handlePushNotification() } } } // 这个是在app外打开链接,实际上不应该用到。 func openURL(urlString: String) { urlToOpen = urlString } func handlePushNotification() { // 确保在主线程中更新视图 DispatchQueue.main.async { let pushInfo = globalEnvironment.pushSettings // 切换到对应的tab if pushInfo.gotoTab >= 0 && pushInfo.gotoTab < 4 { self.appState.selectedTab = pushInfo.gotoTab }else { logger.info("invalid goto Tab: \(pushInfo.gotoTab)") } // 是否显示本地页面 if pushInfo.showPage && pushInfo.page != "" { self.appState.showPromotion = true switch pushInfo.showMode { case "halfScreen" : self.appState.promotionMode = PromotionDisplayType.halfScreen case "fullScreen" : self.appState.promotionMode = PromotionDisplayType.fullScreen default: break } } // 是否显示远程页面 if pushInfo.showPage && pushInfo.openURL != "" { urlToOpen = pushInfo.openURL } logger.info("Push Values. gotoTab: \(pushInfo.gotoTab), showPage: \(pushInfo.showPage), page: \(pushInfo.page), showMode: \(pushInfo.showMode), url: \(pushInfo.openURL)") } } }