How it works
App is the protocol that defines the structure and behavior of a SwiftUI application — its single source of truth for what windows and scenes the program presents. You adopt it on one type, mark that type as the program's entry point, and SwiftUI takes over the run loop, launch, and lifecycle so you never write a main function yourself. Reach for App whenever you build a fully SwiftUI-managed app rather than hosting SwiftUI inside an AppKit or UIKit lifecycle.
Conform a type to
AppAdopting
Appon a struct declares the root of your application. The protocol requires exactly one property,body, that describes the app's scenes; everything the app shows flows from that one declaration. In the example the displayed content lives inAppDemo'sbody, standing in for the root scene's content view.Provide the entry point with
@mainAnnotating your
Apptype with@main— as in@main struct MyApp: App— tells the compiler this type supplies the program's entry point, replacing a hand-writtenmain. SwiftUI synthesizes the launch code, instantiates yourApp, and begins rendering itsbody.Return scenes from
bodyThe
bodyof anAppreturnssome Scene, notsome View: it composes one or moreScenevalues that define the app's windows and their roles. A typical body returns aWindowGroup { ContentView() }, the scene shown in the example's caption, which manages a window whose content is the view hierarchy you supply.Wrap your root view in a scene like
WindowGroupScenes such as
WindowGroupbridgeAppto the view layer by hosting a root view and handling window creation and restoration for you. The view passed toWindowGroup— here theVStackof anImage, a titleText, and the monospacedWindowGroup { ContentView() }label — becomes what the user actually sees inside the app's window.
ContentView() (illustrated here by the VStack body) with a different view to see the whole app launch into new content.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct AppDemo: View {
// An `App` (e.g. `@main struct MyApp: App`) provides the program
// entry point and returns a `Scene` such as `WindowGroup { ContentView() }`.
// Here we render what that root scene's content view looks like.
var body: some View {
VStack(spacing: 12) {
Image(systemName: "app.badge.fill")
.font(.system(size: 44))
.foregroundStyle(.tint)
Text("My App")
.font(.title.bold())
Text("WindowGroup { ContentView() }")
.font(.footnote.monospaced())
.foregroundStyle(.secondary)
}
.padding()
}
}