简体   繁体   English

如何在 iOS 14(Xcode 12 Beta)中从 UIKit 生命周期转换为 SwiftUI 生命周期

[英]How to convert from UIKit life cycle to SwiftUI life cycle in iOS 14 (Xcode 12 Beta)

I am currently working on SwiftUI app in which I am using SceneDelegate and AppDelegate .我目前正在使用 SwiftUI 应用程序,其中我正在使用SceneDelegateAppDelegate I would like to know how I can convert the life cycle from UIKit to SwiftUI one where there is an App struct and with scenes etc.我想知道如何将生命周期从UIKit转换为SwiftUI一个有App结构和scenes等的生命周期。

Also I would like to know how to cater for CoreData and PersistentContainers and inject these into our environments.另外我想知道如何满足 CoreData 和 PersistentContainers 并将它们注入我们的环境。

Also I have used UIApplicationDelegateAdapter to inject AppDelegate but the @main is giving me error我也使用UIApplicationDelegateAdapter注入AppDelegate@main给了我错误

'main()' is only available in iOS 14.0 or newer 'main()' 仅在 iOS 14.0 或更高版本中可用

I am using @available (iOS 14.0, *) in the beginning:我在开始时使用@available (iOS 14.0, *)

import SwiftUI

@available(iOS 14.0, *)
@main

struct MyApp: App {

    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Doing it like this, where does the SceneDelegate code goes.这样做, SceneDelegate代码到哪里去了。 I am still quite confused how this conversion goes.我仍然很困惑这种转换是如何进行的。 I have not seen Apple talking about this in their sessions or anything.我还没有看到苹果在他们的会议上谈论这个或任何事情。 Help will be really appreciated.帮助将不胜感激。

where does the SceneDelegate code goes. SceneDelegate 代码在哪里。

@available(iOS 14.0, *)
@main
struct MyApp: App {

    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    @Environment(\.scenePhase) private var scenePhase

    var body: some Scene {
        WindowGroup {         // << this is a scene
          ContentView()
            .onChange(of: scenePhase) { phase in
              switch phase {
                case .active:
                    print(">> your code is here on scene become active")
                case .inactive:
                    print(">> your code is here on become inactive")
                case .background:
                    print(">> your code is here on go in background")
                default:
                    print(">> do something else in future")
             }
          }
        }
    }
}

Set the environment on the ContentView as follows:在 ContentView 上设置环境如下:

import SwiftUI
import CoreData

@main
struct MasterDetailApp: App {
  @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            ContentView().environment(\.managedObjectContext, appDelegate.persistentContainer.viewContext)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM