How to get the current scene of the iOS14 app developed by XCode 12 and SwiftUI?

Hi! In an iOS 14 app developed with XCode 12 using SwiftUI, I need to access the Scene object of the current view. However, I couldn't find the way.

Some notes online said that I can use expressions such as self.view.window.windowScene to get the scene. However, I cannot find a "window" property in View protocol.

Do you know how to do it?

Thank you!
You may have to maintain a reference to this window, through environment object.

Have a look here:
https://stackoverflow.com/questions/60359808/how-to-access-own-window-within-swiftui-view
Hi Claude31,

The stackoverflow link shows a way that needs to inject hosting window in root ContentWindow from AppDelegate or SceneDelegate. However, in the new Multiplatform App template, there is no AppDelegate/SceneDelegate by default. Seems they are being deprecated. Is there a way to do it without AppDelegate/SceneDelegate?
Thank you!
When you tried:
self.view.window.windowScene

Which object is self ?
Just an update of the status, for now I set the "Enable Multiple Windows" in info.plist to false, such that I don't need to get the scene for now. The original reason I want to get the scene object is because I am trying to use Google Admob in an iOS app for iPad. Since iPad by default supports multiple windows, Admob is asking me to input scene. After disabling multiple windows, AdMob is no longer asing for it.

Google AdMob link is not allowed to be posted in the forum, so I cannot include it.


Hi Claude31,

The expression "self.view.window.windowScene" is from the Google AdMob documentation. Since I am not allowed to post the link in the form, I copy-paste the code section below together with some description.
The documentation doesn't tell what "self" is, but from there I assume the view should have a "window" property. However, I cannot get it when I try to use "self" in the "body" of a View definition.


vvvvvvvvvvvvvvvvvvv from Google AdMob Doc vvvvvvvvvvvvvvvvvvvvvv
Set the scene in an ad request
In order to receive an ad that fits a specific window, you pass the view's windowScene to the ad request. The Google Mobile Ads SDK returns an ad with valid size for that scene.
  • (void)loadInterstitial {

  GADRequest *request = [GADRequest request];
  request.scene = self.view.window.windowScene;

  [self.interstitial loadRequest:request];
}
How to get the current scene of the iOS14 app developed by XCode 12 and SwiftUI?
 
 
Q