Skip to main content
added 2 characters in body
Source Link
Kaunteya
  • 3.1k
  • 1
  • 37
  • 67

I store a reference of the SceneDelegate in a static weak property and initialise it in scene(:willConnectTo:options)

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    static weak var shared: SceneDelegate?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        Self.shared = self
    }
}

Later, scene can be accessed using SceneDelegate.shared


NoteUPDATE: As mentioned in the comments, this solution will only work for apps that deal with only one scene. Apps that run on iPhone typically have only one scene. iPad apps can have multiple scenes.

I store a reference of the SceneDelegate in a static weak property and initialise it in scene(:willConnectTo:options)

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    static weak var shared: SceneDelegate?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        Self.shared = self
    }
}

Later, scene can be accessed using SceneDelegate.shared


Note: As mentioned in the comments, this solution will only work for apps that deal with only one scene. Apps that run on iPhone typically have only one scene. iPad apps can have multiple scenes.

I store a reference of the SceneDelegate in a static weak property and initialise it in scene(:willConnectTo:options)

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    static weak var shared: SceneDelegate?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        Self.shared = self
    }
}

Later, scene can be accessed using SceneDelegate.shared


UPDATE: As mentioned in the comments, this solution will only work for apps that deal with only one scene. Apps that run on iPhone typically have only one scene. iPad apps can have multiple scenes.

added note of multiple scene
Source Link
Kaunteya
  • 3.1k
  • 1
  • 37
  • 67

I store a reference of the SceneDelegate in a static weak property and initialise it in scene(:willConnectTo:options)

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    static weak var shared: SceneDelegate?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        Self.shared = self
    }
}

Later, scene can be accessed using SceneDelegate.shared


Note: As mentioned in the comments, this solution will only work for apps that deal with only one scene. Apps that run on iPhone typically have only one scene. iPad apps can have multiple scenes.

I store a reference of the SceneDelegate in a static weak property and initialise it in scene(:willConnectTo:options)

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    static weak var shared: SceneDelegate?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        Self.shared = self
    }
}

Later, scene can be accessed using SceneDelegate.shared

I store a reference of the SceneDelegate in a static weak property and initialise it in scene(:willConnectTo:options)

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    static weak var shared: SceneDelegate?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        Self.shared = self
    }
}

Later, scene can be accessed using SceneDelegate.shared


Note: As mentioned in the comments, this solution will only work for apps that deal with only one scene. Apps that run on iPhone typically have only one scene. iPad apps can have multiple scenes.

fixed grammar
Source Link
Kaunteya
  • 3.1k
  • 1
  • 37
  • 67

I store thea reference of the SceneDelegate in a static weak property and initialise it in scene(:willConnectTo:options)

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    static weak var shared: SceneDelegate?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        Self.shared = self
    }
}

Later, scene can be accessed using SceneDelegate.shared

I store the reference of SceneDelegate in a static weak property and initialise it in scene(:willConnectTo:options)

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    static weak var shared: SceneDelegate?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        Self.shared = self
    }
}

Later scene can be accessed using SceneDelegate.shared

I store a reference of the SceneDelegate in a static weak property and initialise it in scene(:willConnectTo:options)

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    static weak var shared: SceneDelegate?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        Self.shared = self
    }
}

Later, scene can be accessed using SceneDelegate.shared

Source Link
Kaunteya
  • 3.1k
  • 1
  • 37
  • 67
Loading