How to Use Uiwindowscene.Windows on iOS 15

How to get rid of message 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead with AdMob banner?

You can use following as UIApplication.shared.currentUIWindow()?.rootViewController

public extension UIApplication {
func currentUIWindow() -> UIWindow? {
let connectedScenes = UIApplication.shared.connectedScenes
.filter { $0.activationState == .foregroundActive }
.compactMap { $0 as? UIWindowScene }

let window = connectedScenes.first?
.windows
.first { $0.isKeyWindow }

return window

}
}

Why it is throw an error as 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead in SwiftUI?

Well, the warning message reflects the essence of the problem pretty fully.

Apple really deprecated UIApplication.shared.windows, so to fix your warning, instead of UIApplication.shared.windows.first? you should use:

UIApplication
.shared
.connectedScenes
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
.first { $0.isKeyWindow }

Then, your .padding view modifier will look like this:

.padding(.top, UIApplication
.shared
.connectedScenes
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
.first { $0.isKeyWindow }?.safeAreaInsets.top)

windows' was deprecated in iOS 15.0

this has been updated for iOS 15

UIApplication
.shared
.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap { $0.windows }
.first { $0.isKeyWindow }
.rootViewController?.present()


Related Topics



Leave a reply



Submit