Skip to content

Instantly share code, notes, and snippets.

@sebj
Last active April 8, 2025 18:04
Show Gist options
  • Select an option

  • Save sebj/bae35d853be780434a894b62e21faaff to your computer and use it in GitHub Desktop.

Select an option

Save sebj/bae35d853be780434a894b62e21faaff to your computer and use it in GitHub Desktop.

Revisions

  1. sebj revised this gist Nov 18, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Guide.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ See also: [`UIApplicationDelegate` documentation](https://developer.apple.com/do

    ```swift
    // AppDelegate.swift
    @UIApplicationMain
    @main
    final class AppDelegate: NSObject, UIApplicationDelegate {

    func application(
  2. sebj revised this gist Jan 10, 2023. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Guide.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    ## Define an application delegate
    See also: [`UIApplicationDelegate` documentation](https://developer.apple.com/documentation/uikit/uiapplicationdelegate)

    ```swift
    // AppDelegate.swift
    @UIApplicationMain
    @@ -17,6 +19,8 @@ final class AppDelegate: NSObject, UIApplicationDelegate {
    ```

    ## Define a scene delegate
    See also: [`UIWindowSceneDelegate` documentation](https://developer.apple.com/documentation/uikit/uiwindowscenedelegate)

    ```swift
    // SceneDelegate.swift
    final class SceneDelegate: NSObject, UIWindowSceneDelegate {
  3. sebj revised this gist Jan 10, 2023. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions Guide.md
    Original file line number Diff line number Diff line change
    @@ -35,8 +35,7 @@ final class SceneDelegate: NSObject, UIWindowSceneDelegate {
    let window = UIWindow(windowScene: windowScene)

    // TODO: Do something with `window`
    let contentView = ContentView()

    let contentView = ContentView() // or whatever your root SwiftUI view is called
    window.rootViewController = UIHostingController(rootView: contentView)
    window.makeKeyAndVisible()

  4. sebj revised this gist Jan 10, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Guide.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    ## Define an application delegate
    ```swift
    // AppDelegate.swift
    @UIApplicationMain
    final class AppDelegate: NSObject, UIApplicationDelegate {

    @@ -17,6 +18,7 @@ final class AppDelegate: NSObject, UIApplicationDelegate {

    ## Define a scene delegate
    ```swift
    // SceneDelegate.swift
    final class SceneDelegate: NSObject, UIWindowSceneDelegate {

    var window: UIWindow?
  5. sebj revised this gist Jan 10, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Guide.md
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ final class SceneDelegate: NSObject, UIWindowSceneDelegate {

    ## Specify the scene delegate as part of the app's Info.plist file

    In a newly created project:
    In a newly created SwiftUI project:
    1. Click your project name in the left file navigator
    2. Click the app target in the following screen
    3. Click 'Info'
  6. sebj revised this gist Jan 10, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Guide.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ final class AppDelegate: NSObject, UIApplicationDelegate {
    configurationForConnecting connectingSceneSession: UISceneSession,
    options: UIScene.ConnectionOptions
    ) -> UISceneConfiguration {
    let sceneConfiguration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
    let sceneConfiguration = UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    sceneConfiguration.delegateClass = SceneDelegate.self
    return sceneConfiguration
    }
  7. sebj created this gist Jan 10, 2023.
    74 changes: 74 additions & 0 deletions Guide.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    ## Define an application delegate
    ```swift
    @UIApplicationMain
    final class AppDelegate: NSObject, UIApplicationDelegate {

    func application(
    _ application: UIApplication,
    configurationForConnecting connectingSceneSession: UISceneSession,
    options: UIScene.ConnectionOptions
    ) -> UISceneConfiguration {
    let sceneConfiguration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
    sceneConfiguration.delegateClass = SceneDelegate.self
    return sceneConfiguration
    }
    }
    ```

    ## Define a scene delegate
    ```swift
    final class SceneDelegate: NSObject, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(
    _ scene: UIScene,
    willConnectTo session: UISceneSession,
    options connectionOptions: UIScene.ConnectionOptions
    ) {
    guard let windowScene = scene as? UIWindowScene else {
    return
    }

    let window = UIWindow(windowScene: windowScene)

    // TODO: Do something with `window`
    let contentView = ContentView()

    window.rootViewController = UIHostingController(rootView: contentView)
    window.makeKeyAndVisible()

    self.window = window
    }
    }
    ```

    ## Specify the scene delegate as part of the app's Info.plist file

    In a newly created project:
    1. Click your project name in the left file navigator
    2. Click the app target in the following screen
    3. Click 'Info'
    4. Expand 'Application Scene Manifest'
    5. Add a row inside 'Application Scene Manifest' – Xcode will generate a `<Your-Target-Name>-Info.plist` file in the left file navigator
    6. Right click on the generated plist file, and choose Open As → Source Code
    7. Add the scene configuration below inside the `<dict>` for `UIApplicationSceneManifest`

    In a project with an existing Info.plist file:
    1. Right click on the plist file in the left file navigator, and choose Open As → Source Code
    2. Add the scene configuration below inside the `<dict>` for `UIApplicationSceneManifest`

    ```
    <key>UISceneConfigurations</key>
    <dict>
    <key>UIWindowSceneSessionRoleApplication</key>
    <array>
    <dict>
    <key>UISceneConfigurationName</key>
    <string>Default Configuration</string>
    <key>UISceneDelegateClassName</key>
    <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
    </dict>
    </array>
    </dict>
    ```