'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows?

With the following code I get the warning below it:

let rootController = UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.rootViewController

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

How do I change the code above to work with UIWindowScene instead?

That depends on where you are calling this code from. This is just grabbing the first key window that is found in the whole application, which might not belong to the window scene the view hierarchy belongs to that you are dealing with.

If you have a view, the easiest way to access the key window of your scene is to call view.windowScene.keyWindow

@JimmyCricket Is this for something like document revision conflict? Ideally there'd be a mapping somewhere in your app between iCloud documents and scenes (perhaps with UISceneSession's persistentIdentifier). Then you would know exactly what scene any sort of alert should be presented on.

This is the best type of solution in this case. Tie information together and plumb it where you need it so you can make the right decision. Since iOS 13, we've had to help teams internally do the same, often by adding a UIWindowScene parameter in their API so they have context as to where the interaction took place.

That being said, sometimes doing that level of refactor isn't always possible. The solution above will continue to work. It is just deprecated. Just note that the solution above is guaranteed to be wrong some percentage of the time as the application-level key window is not totally within your control.

How do i replace the below code: UIApplication.shared.windows.first?.overrideUserInterfaceStyle = .unspecified

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

((UIWindowScene *)([UIApplication sharedApplication].connectedScenes.allObjects[0])).statusBarManager.statusBarFrame;

In Objective C I was able to replace:

NSArray  *windows = [[UIApplication sharedApplication] windows];

with

NSArray *scenes=[[[UIApplication sharedApplication] connectedScenes] allObjects];
NSArray *windows=[[scenes objectAtIndex:0] windows];

I follow that with:

   for (UIWindow  *window in windows) {
        if (window.isKeyWindow) {
            foundWindow = window;
            break;
        }
   }
   UIViewController* parentController = foundWindow.rootViewController;
   while( parentController.presentedViewController &&
          parentController != parentController.presentedViewController ){
          parentController = parentController.presentedViewController;
   }

  if let window = UIApplication.shared.connectedScenes.map({ $0 as? UIWindowScene }).compactMap({ $0 }).first?.windows.first {             } try this solution it's work for me

let allScenes = UIApplication.shared.connectedScenes
let scene = allScenes.first { $0.activationState == .foregroundActive }
                        
if let windowScene = scene as? UIWindowScene {
         windowScene.keyWindow?.rootViewController?.present(SFSafariViewController(url: url, configuration: conf), animated: isAnimated, completion: nil)
}
static var keyWindow: UIWindow? {
  let allScenes = UIApplication.shared.connectedScenes
  for scene in allScenes {
    guard let windowScene = scene as? UIWindowScene else { continue }
    for window in windowScene.windows where window.isKeyWindow {
       return window
     }
   }
    return nil
}
    static var keyWindow: UIWindow? {
        guard let window = UIApplication.shared.connectedScenes
            .compactMap({ $0 as? UIWindowScene })
            .flatMap({ $0.windows })
            .first(where: { $0.isKeyWindow })
        else {
            return nil
        }
        return window
    }
'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows?
 
 
Q