Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead #1

Open
ljunquera opened this issue Jan 13, 2022 · 1 comment

Comments

@ljunquera
Copy link

I am getting this warning in the IDE:

'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead

on this line of code:

let window = UIApplication.shared.windows.filter { $0.isKeyWindow }.first

and when I try:

let window = UIWindowScene.windows.filter { $0.isKeyWindow }.first

I get this error in the IDE:

Instance member 'windows' cannot be used on type 'UIWindowScene'; did you mean to use a value of this type instead?

@alschmut
Copy link
Owner

@ljunquera Thanks for documenting this issue!

Seems like there is a way to get the top UIViewController from the current scene, using a SceneDelegate, set inside an AppDelegate, which again is injected into the SwiftUI app. To me this doesn't look like the most beautiful solution, but maybe it helps for now.

import SwiftUI
import UIKit

@main
struct AceLifeApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            Text("Hello World!")
        }
    }
}
    
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(
        _ application: UIApplication,
        configurationForConnecting connectingSceneSession: UISceneSession,
        options: UIScene.ConnectionOptions
    ) -> UISceneConfiguration {
        let sceneConfig = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
        sceneConfig.delegateClass = SceneDelegate.self
        return sceneConfig
    }
}

class SceneDelegate: NSObject, UIWindowSceneDelegate {
    let msAuthAdapter: MSAuthAdapterProtocol = resolve()
    
    func sceneWillEnterForeground(_ scene: UIScene) {
        if
            let delegate = (scene as? UIWindowScene)?.delegate,
            let windowSceneDelegate = delegate as? UIWindowSceneDelegate,
            let topViewController = windowSceneDelegate.window??.rootViewController
        {
            msAuthAdapter.setupViewConnection(with: topViewController)
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants