Skip to content

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

@ToddHoff

Description

@ToddHoff

First, thanks for this code. It's great and has been a great help.

In new releases there's a new warning in the build:

func setFrame() {

This line has this warning: 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead:
let safeAreaInsets = UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.safeAreaInsets ?? .zero
let frame = UIScreen.main.bounds.inset(by: safeAreaInsets)

    let adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(frame.width)
    
    self.width = adSize.size.width
    self.height = adSize.size.height
}

After a lot of research I didn't find a clear answer. I fixed it with:

func getSafeArea()-> UIEdgeInsets {

    let keyWindow = UIApplication
        .shared
        .connectedScenes
        .flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
        .first { $0.isKeyWindow }
    
    return (keyWindow?.safeAreaInsets)!
}

func setFrame() {
    
     let safeAreaInsets = getSafeArea()
    let frame = UIScreen.main.bounds.inset(by: safeAreaInsets)
    
    let adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(frame.width)
    
    self.width = adSize.size.width
    self.height = adSize.size.height
     
}

This fixes the warning and seems to mostly work, but I get a few crashes in getSafeArea() with an unwrapped nil error. I can see why that would be, but since you actually know what you're doing, I'm just wondering if there's a better fix?

thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions