3

I wanna make some app on macOS using SwfitUI.

Following the iOS, there is SceneDelegate (after iOS 13). But in macOS, there is only AppDelegate.

OK. I wanna use environmentObject on SceneDelegate's scene method

window.rootViewController = UIHostingController(rootView: contentView .environmentObject(mainController) ) 

to use @EnvironmentObject property wrapper in my ContentView.

Literally I can't implement .environmentObject() cuz in macOS there is no SceneDelegate!

How can I solve this problem?

1 Answer 1

3

Here is default generated AppDelegate for macOS SwiftUI project, and how environment object can be set for content view in it.

func applicationDidFinishLaunching(_ aNotification: Notification) { // Create content view let contentView = ContentView().environmentObject(AppSettings()) // Create the window and set the content view. window = NSWindow( contentRect: NSRect(x: 0, y: 0, width: 800, height: 600), styleMask: [.titled, .closable, .miniaturizable], backing: .buffered, defer: false) window.center() window.setFrameAutosaveName("Main Window") window.contentView = NSHostingView(rootView: contentView) window.makeKeyAndOrderFront(nil) window.autorecalculatesKeyViewLoop = true } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.