如何在SwiftUI中使用@EnvironmentObject在AppDelegate和SceneDelegate / View之间共享数据

时间:2019-07-17 17:27:08

标签: appdelegate swiftui

在SwiftUI 5.1中,我想使用AppDelegate创建一个userData对象。 userData也将包含BLE广告数据,该数据也将从AppDelegate更新。这些数据应可用于UI来显示这些值。

我在AppDelegate中使用

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    private var centralManager : CBCentralManager!
    var userData: UserData!

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        userData = UserData()

        return true
    }

在SceneDelegate中,我想使用

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    @EnvironmentObject var userData: UserData

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

        // Use a UIHostingController as window root view controller
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView:
                BeaconList().environmentObject(userData)
            )

            self.window = window
            window.makeKeyAndVisible()
        }
    }

编译正常,但是运行代码时我得到

  

线程1:致命错误:在外部读取EnvironmentObject   View.body

如果我删除

  

.environmentObject(userData)

我知道

  

线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)

本质上,我试图从AppDelegate创建和更新userData对象,并从SceneDelegate显示并在下面查看。

我该如何实现?

1 个答案:

答案 0 :(得分:0)

也许您可以使用UIApplication.shared.delegate来使AppDelegate访问用户数据:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var userData: UserData!

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        userData = UserData()
        return true
    }
}
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

        let userData = (UIApplication.shared.delegate as! AppDelegate).userData

        // Use a UIHostingController as window root view controller
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: ContentView().environmentObject(userData))
            self.window = window
            window.makeKeyAndVisible()
        }
    }