uikit_ui_util.mm 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #import "ios/web/common/uikit_ui_util.h"
  5. #import <UIKit/UIKit.h>
  6. #if !defined(__has_feature) || !__has_feature(objc_arc)
  7. #error "This file requires ARC support."
  8. #endif
  9. UIWindow* GetAnyKeyWindow() {
  10. // In iOS 15 and later key windows are a deprecated concept. Window state
  11. // should be determined at the scene rather than the application level.
  12. if (@available(iOS 15, *)) {
  13. NSSet<UIScene*>* windowScenes =
  14. [UIApplication sharedApplication].connectedScenes;
  15. for (UIScene* scene : windowScenes) {
  16. if ([scene.delegate
  17. conformsToProtocol:@protocol(UIWindowSceneDelegate)]) {
  18. return [(id<UIWindowSceneDelegate>)scene.delegate window];
  19. }
  20. }
  21. } else {
  22. NSArray<UIWindow*>* windows = [UIApplication sharedApplication].windows;
  23. // Find a key window if it exists.
  24. for (UIWindow* window in windows) {
  25. if (window.isKeyWindow)
  26. return window;
  27. }
  28. }
  29. return nil;
  30. }
  31. UIInterfaceOrientation GetInterfaceOrientation() {
  32. return GetAnyKeyWindow().windowScene.interfaceOrientation;
  33. }