美文网首页
SwiftUI App 增加 AppDelegate

SwiftUI App 增加 AppDelegate

作者: 透支未来 | 来源:发表于2021-08-12 16:04 被阅读0次

iOS 14中,SwiftUI apps遵循App protocol,没有遵循UIApplicationDelegate,但是一些场景需要用到旧的Appdelegate中的生命周期函数,比如push注册,内存警告处理等。
1.首先,创建一个类遵循UIApplicationDelegate

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        return true
    }
}

这里以didFinishLaunchingWithOptions为例,可以增加任何UIApplicationDelegate包含的函数。

2.使用UIApplicationDelegateAdaptor属性修饰器,指定你创建的Appdelegate

@main
struct CZHSwfitUIApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

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

相关文章

网友评论

      本文标题:SwiftUI App 增加 AppDelegate

      本文链接:https://www.haomeiwen.com/subject/piplbltx.html