UIScreen.main is deprecated

We have unit test targets that do not need a host app.

However, when running snapshot tests (using frameworks such as swift-snapshot-testing), we'd like to read some properties on UIScreen.main.

For instance, we'd like to assert that the scale of the simulator that's being used is matching our expectation. Without using a host app, there's no connected UIScene on UIWindow.shared.connectedScenes.

Questions:

  1. Why UIScreen.main was deprecated?
  2. In our case, how can we get the scale property without attaching a host app to our test target? In other words, without using UIWindow.shared.connectedScenes.
Answered by Frameworks Engineer in 892323022

We have unit test targets that do not need a host app.

Without using a host app, there's no connected UIScene on UIWindow.shared.connectedScenes.

I understand model-level test targets not requiring a host app, but if you are testing anything UI related - even just scale as you mentioned - you really need a host. Testing UI and not having a UIWindowScene is really quite invalid.

A scene is a user-level "window" but also the system provided view-port and connection to your app. Without that, all manner of things will be invalid and not set. Having tests run in this sort of environment will give you a false sense of security. They may pass, but it is not indicative of an actual environment this code would see itself in when run with a UIWindowScene connected.

  1. Why UIScreen.main was deprecated?

In the world of multiple scenes (CarPlay, iPad, Mac Catalyst, etc.) it is always more appropriate to obtain information from a UIScreen contextually. Allowing code to reach and find a global object like UIScreen.main is convenient, but not the best abstraction long term. It is better to think in terms of scenes, and passing context into code that needs it (such as a UIScreen from view.window.windowScene.screen).

Better yet, for something like scale, you should look into replacing UIScreen.scale with the displayScale trait:

https://developer.apple.com/documentation/uikit/uitraitcollection/displayscale

With automatic trait tracking, you can set up observation and be notified when it changes:

https://developer.apple.com/documentation/uikit/automatic-trait-tracking

We cover these topics and more in the "Modernize your UIKit app" from this year:

https://developer.apple.com/videos/play/wwdc2026/278/

  1. In our case, how can we get the scale property without attaching a host app to our test target?

See above. If you are testing anything UI related, like scale, you really need to have a UIWindowScene present. More than just scale will be invalid if you don't.

We have unit test targets that do not need a host app.

Without using a host app, there's no connected UIScene on UIWindow.shared.connectedScenes.

I understand model-level test targets not requiring a host app, but if you are testing anything UI related - even just scale as you mentioned - you really need a host. Testing UI and not having a UIWindowScene is really quite invalid.

A scene is a user-level "window" but also the system provided view-port and connection to your app. Without that, all manner of things will be invalid and not set. Having tests run in this sort of environment will give you a false sense of security. They may pass, but it is not indicative of an actual environment this code would see itself in when run with a UIWindowScene connected.

  1. Why UIScreen.main was deprecated?

In the world of multiple scenes (CarPlay, iPad, Mac Catalyst, etc.) it is always more appropriate to obtain information from a UIScreen contextually. Allowing code to reach and find a global object like UIScreen.main is convenient, but not the best abstraction long term. It is better to think in terms of scenes, and passing context into code that needs it (such as a UIScreen from view.window.windowScene.screen).

Better yet, for something like scale, you should look into replacing UIScreen.scale with the displayScale trait:

https://developer.apple.com/documentation/uikit/uitraitcollection/displayscale

With automatic trait tracking, you can set up observation and be notified when it changes:

https://developer.apple.com/documentation/uikit/automatic-trait-tracking

We cover these topics and more in the "Modernize your UIKit app" from this year:

https://developer.apple.com/videos/play/wwdc2026/278/

  1. In our case, how can we get the scale property without attaching a host app to our test target?

See above. If you are testing anything UI related, like scale, you really need to have a UIWindowScene present. More than just scale will be invalid if you don't.

UIScreen.main is deprecated
 
 
Q