4

I want to get rid of the " 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead" message in my function (my function works very well by the way.).

The line of code involved:

banner.rootViewController = UIApplication.shared.windows.first?.rootViewController

How can i do that?

My function :

struct AdView : UIViewRepresentable {
    func makeUIView(context: UIViewRepresentableContext<AdView>) -> GADBannerView {
        let banner = GADBannerView(adSize: kGADAdSizeBanner)
        banner.adUnitID = "ca-app-pub-3940256099942544/2934735716" // test ad
                banner.rootViewController = UIApplication.shared.windows.first?.rootViewController
        banner.load(GADRequest())
        return banner
    }
    
    func updateUIView(_ uiView: GADBannerView, context: UIViewRepresentableContext<AdView>) {}
}

I tried that but it's not working :

banner.rootViewController = UIApplication.shared.connectedScenes.first?.rootViewController

Thanks

Flincorp
  • 751
  • 9
  • 22

2 Answers2

16

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
        
    }
}
cluelessCoder
  • 948
  • 6
  • 19
  • Thank you for the catch! Correct now, I've just copied the code I'm using via iPad (with some simplification) and forgot to remove it. – cluelessCoder Oct 23 '21 at 10:05
  • 1
    This seems to happen later than using `UIWindowScene.windows.first?`, unfortunately. Using `UIApplication.shared.connectedScenes.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }.first { $0.isKeyWindow }` does the trick. – Lucas Bento Jan 01 '23 at 14:08
  • Can you comment more on “this seems to happen later”? – cluelessCoder Jan 01 '23 at 17:42
1

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];

My full method is:

-(UIViewController *)parentController {
    
    UIWindow *foundWindow = nil;
    NSArray *scenes=[[[UIApplication sharedApplication] connectedScenes] allObjects];
    NSArray *windows=[[scenes objectAtIndex:0] windows];
    for (UIWindow  *window in windows) {
        if (window.isKeyWindow) {
            foundWindow = window;
            break;
        }
    }
    UIViewController* parentController = foundWindow.rootViewController;
    while( parentController.presentedViewController &&
          parentController != parentController.presentedViewController ){
        parentController = parentController.presentedViewController;
    }
    return  parentController;
}
Jonathan
  • 3
  • 4
Peter B. Kramer
  • 16,385
  • 1
  • 16
  • 20