TechnologiesSwiftUI

App protocol

iOSmacOStvOSwatchOSvisionOSiOS 14.0+✓ renders

A type that represents the structure and behavior of an app.

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.

  1. Conform a type to App

    Adopting App on 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 in AppDemo's body, standing in for the root scene's content view.

  2. Provide the entry point with @main

    Annotating your App type with @main — as in @main struct MyApp: App — tells the compiler this type supplies the program's entry point, replacing a hand-written main. SwiftUI synthesizes the launch code, instantiates your App, and begins rendering its body.

  3. Return scenes from body

    The body of an App returns some Scene, not some View: it composes one or more Scene values that define the app's windows and their roles. A typical body returns a WindowGroup { ContentView() }, the scene shown in the example's caption, which manages a window whose content is the view hierarchy you supply.

  4. Wrap your root view in a scene like WindowGroup

    Scenes such as WindowGroup bridge App to the view layer by hosting a root view and handling window creation and restoration for you. The view passed to WindowGroup — here the VStack of an Image, a title Text, and the monospaced WindowGroup { ContentView() } label — becomes what the user actually sees inside the app's window.

Try it — Change the root view inside the scene by replacing 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.

App.swift
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()
    }
}
Live preview
My App WindowGroup { ContentView() }
My App WindowGroup { ContentView() }
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →