SwiftUI 应用生命周期 iOS14 AppDelegate 代码放在哪里?

Rex*_*xha 74 ios firebase swiftui ios14 xcode12

现在,AppDelegateSceneDelegate从SwiftUI删除,你在哪里我把我以前在代码SceneDelegateAppDelegate,火力地堡配置为前?

所以我目前在我的代码中有这个代码AppDelegate

我现在应该把这段代码放在哪里?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    FirebaseConfiguration.shared.setLoggerLevel(.min)
    FirebaseApp.configure()
    return true
}
Run Code Online (Sandbox Code Playgroud)

Asp*_*eri 67

这是 SwiftUI 生命周期的解决方案。使用 Xcode 12b / iOS 14 测试

import SwiftUI
import UIKit

// no changes in your AppDelegate class
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        print(">> your code here !!")
        return true
    }
}

@main
struct Testing_SwiftUI2App: App {

    // inject into SwiftUI life-cycle via adaptor !!!
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @RexhinHoxha,将部署目标设置为 iOS 14,从 AppDelegate 中删除“@UIApplicationMain”,并添加 is-a“App” 结构,如上所示。 (5认同)

Pet*_*ese 54

覆盖您的初始化程序App也有效:

import SwiftUI
import Firebase

@main
struct BookSpineApp: App {
  
  init() {
    FirebaseApp.configure()
  }
  
  var body: some Scene {
    WindowGroup {
      BooksListView()
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在这里找到更详细的文章:

  • 我注意到,使用此功能时,我在控制台上收到警告:[GoogleUtilities/AppDelegateSwizzler][I-SWZ001014] App Delegate 不符合 UIApplicationDelegate 协议。 (4认同)

Moj*_*ini 39

你根本不应该把那种代码放在 app delegate 中,否则你最终会面对Massive App Delegate。相反,您应该考虑将代码重构为更有意义的部分,然后将正确的部分放在正确的位置。对于这种情况,您唯一需要做的就是确保一旦应用程序准备就绪,代码就会执行这些功能,并且只执行一次。所以这个init方法可能很棒:

@main
struct MyApp: App {
    init() {
        setupFirebase()
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

private extension MyApp {
    func setupFirebase() {
        FirebaseConfiguration.shared.setLoggerLevel(.min)
        FirebaseApp.configure()
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序委托?

您可以拥有自己的自定义类并将其分配为delegate. 但请注意,它不适用于分配之前发生的事件。例如:

class CustomDelegate: NSObject, UIApplicationDelegate {
    static let Shared = CustomDelegate()
}
Run Code Online (Sandbox Code Playgroud)

然后:

UIApplication.shared.delegate = CustomDelegate.Shared
Run Code Online (Sandbox Code Playgroud)

观察通知

大多数AppDelegate方法实际上是在观察通知,您可以手动观察这些通知,而不是定义一个新类。例如:

NotificationCenter.default.addObserver(
    self,
    selector: #selector(<#T##@objc method#>),
    name: UIApplication.didBecomeActiveNotification,
    object: nil
)
Run Code Online (Sandbox Code Playgroud)

原生AppDelegate包装器

您可以直接将应用程序委托注入到@main结构中:

@UIApplicationDelegateAdaptor(CustomDelegate.self) var appDelegate
Run Code Online (Sandbox Code Playgroud)

注意:使用 AppDelegate

请记住,添加 AppDelegate 意味着您正在取消默认的多平台支持,您必须手动检查平台。


MSc*_*ler 15

您还可以将新的 ScenePhase 用于 AppDelegate 和 SceneDelegate 的某些代码。喜欢去后台或变得活跃。从

struct PodcastScene: Scene {
    @Environment(\.scenePhase) private var phase

    var body: some Scene {
        WindowGroup {
            TabView {
                LibraryView()
                DiscoverView()
                SearchView()
            }
        }
        .onChange(of: phase) { newPhase in
            switch newPhase {
            case .active:
                // App became active
            case .inactive:
                // App became inactive
            case .background:
                // App is running in the background
            @unknown default:
                // Fallback for future cases
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

示例信用:https : //wwdcbysundell.com/2020/building-entire-apps-with-swiftui/