.main is going to be deprecated

I have this code in the sceneDelegate of my app. I understand that .main is going to be deprecated. The warning says to use view?.window?.windowScene?.screen but there is no view in sceneDelegate. How can I change

let screenSize = UIScreen.main.fixedCoordinateSpace.bounds

to not use .main?

Thanks in advance.

Could you show more code ?

In which func do you use it ?

Please also show the exact warning.

I did not get any warning in Xcode 14.2 with the following code:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    @available(iOS 13.0, *)
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
        let screenSize = UIScreen.main.fixedCoordinateSpace.bounds
        print("screenSize", screenSize)
    }

And I get size in the log:

screenSize (0.0, 0.0, 393.0, 852.0)

I apologize for misspeaking. I don't get a true warning. When I type .main the auto select box shows the "alert". I am also running Xcode 14.2. I'm just trying to be proactive and fix this before it gets broken in the future and I'm currently updating this app. Typing view.window.windowScene.screen in the sceneDelegate shows a warning though.

OK, I got the warning.

As advised, I replaced by

        guard let w = (scene as? UIWindowScene) else { return }
        let screenSize = w.screen.bounds
        print("screenSize", screenSize)

and got the expected result:

screenSize (0.0, 0.0, 393.0, 852.0)

Or

        let screenSize = (window?.screen.bounds)!
        print("screenSize", screenSize)

Or

        let screenSize = (window?.windowScene!.screen.bounds)!
        print("screenSize", screenSize)

And got the same result.

Thanks a lot, Claude. Just what I needed.

OOOps! I misspoke again. I use the same line of code in appDelegate. However, obviously, there's no scene in appDelegate. How can I handle this?

Thanks a lot for the help.

Why do you want to do it specifically in AppDelegate and not Scene Delegate ?

  • if you have a sceneDelegate, then you should do it there as shown before
  • if not, you have a window declared in AppDelegate.
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?  
  • In didFinishLaunchingWithOptions, you can call:
        let firstScene = window?.windowScene
        if let w = firstScene as? UIWindowScene {
            let screenSize = w.screen.bounds
            print("screenSize", screenSize)
        }
  • You can also do it in any VC (viewDidLoad for instance):
          let firstScene = UIApplication.shared.connectedScenes.first // may be nil
          if let w = firstScene as? UIWindowScene {
              let screenSize = w.screen.bounds
              print("screenSize", screenSize)
          }

I tested the different cases. It worked. So please post the complete project or post an email (I will reply with mine) in order to send your project directly.

Claude, Here's the code from my appDelegate.

import UIKit

var gScreenSize = CGRect()
var gScreenHeight: CGFloat = 0.0
var gScreenWidth: CGFloat = 0.0
var gOrientation: String = ""

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Override point for customization after application launch.
        NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.rotated), name: UIDevice.orientationDidChangeNotification, object: nil)

        return true
    }

    @objc func rotated()
    {
        let theCurrentDevice = UIDevice.current
        
        if theCurrentDevice.orientation.isValidInterfaceOrientation
        {
            let deviceOrientationRaw = theCurrentDevice.orientation.rawValue
            switch deviceOrientationRaw
            {
            case 1: // Home Button at the Bottom
                gScreenSize = UIScreen.main.fixedCoordinateSpace.bounds
                
                let firstScene = window?.windowScene
                if let w = firstScene
                {
                    // gScreenSize = w.screen.bounds
                    print("portrait bottom", w.screen.bounds)
                }

                 print("Portrait AppDelegate height", gScreenSize.height)
                 print("Portrait AppDelegate width", gScreenSize.width)
               
                gOrientation = "Portrait" // K.Oreientation.portrait
                gScreenHeight = gScreenSize.height
                gScreenWidth = gScreenSize.width
                
            case 2: // Home Button at the Top
                gScreenSize = UIScreen.main.fixedCoordinateSpace.bounds
                
                let firstScene = window?.windowScene
                if let w = firstScene
                {
                    // gScreenSize = w.screen.bounds
                    print("portrait top", w.screen.bounds)
                }
                
                 print("Portrait AppDelegate height", gScreenSize.height)
                 print("Portrait AppDelegate width", gScreenSize.width)
                
                gOrientation = "Portrait" // K.Oreientation.portrait
                gScreenHeight = gScreenSize.height
                gScreenWidth = gScreenSize.width
                
            case 3: // Home Button on the Right
                gScreenSize = UIScreen.main.fixedCoordinateSpace.bounds
                
                let firstScene = window?.windowScene
                if let w = firstScene
                {
                    // gScreenSize = w.screen.bounds
                    print("Landscape right", w.screen.bounds)
                }
                
                 print("Landscape Home Right AppDelegate height", gScreenSize.width)
                 print("Landscape Home Right AppDelegate width", gScreenSize.height)
                
                gOrientation = "Landscape" // K.Oreientation.landscape
                gScreenHeight = gScreenSize.width
                gScreenWidth = gScreenSize.height
                
            case 4: // Home Button on the Left
                gScreenSize = UIScreen.main.fixedCoordinateSpace.bounds
                
                let firstScene = window?.windowScene
                if let w = firstScene
                {
                    // gScreenSize = w.screen.bounds
                    print("Landscape left", w.screen.bounds)
                }
                
                 print("Landscape Home Left AppDelegate height", gScreenSize.width)
                 print("Landscape Home Left AppDelegate width", gScreenSize.height)
                
                gOrientation = "Landscape" // K.Oreientation.landscape
                gScreenHeight = gScreenSize.width
                gScreenWidth = gScreenSize.height
                
            case 5:
                //deviceOrreientation = "Face Up"
                break
                
            case 6:
                //deviceOrreientation = "Face Down"
                break
                
            default:
                // gOrientation_2 = K.Oreientation.unknown
                break
            }
        }
    }
    // MARK: UISceneSession Lifecycle
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
}
.main is going to be deprecated
 
 
Q