Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ios): update the way to get keyWindow #3448

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ios/Video/Features/RCTVideoUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,16 @@ enum RCTVideoUtils {

return nil
}

static func getCurrentWindow() -> UIWindow? {
if #available(iOS 13.0, tvOS 13, *) {
return UIApplication.shared.connectedScenes
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
.last { $0.isKeyWindow }
} else {
#if !os(visionOS)
return UIApplication.shared.keyWindow
#endif
}
}
}
10 changes: 7 additions & 3 deletions ios/Video/RCTVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,8 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
// Find the nearest view controller
var viewController: UIViewController! = self.firstAvailableUIViewController()
if viewController == nil {
let keyWindow: UIWindow! = UIApplication.shared.keyWindow
guard let keyWindow = RCTVideoUtils.getCurrentWindow() else { return }

viewController = keyWindow.rootViewController
if !viewController.children.isEmpty {
viewController = viewController.children.last
Expand Down Expand Up @@ -1291,9 +1292,12 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
func handleViewControllerOverlayViewFrameChange(overlayView _: UIView, change: NSKeyValueObservedChange<CGRect>) {
let oldRect = change.oldValue
let newRect = change.newValue

guard let bounds = RCTVideoUtils.getCurrentWindow()?.bounds else { return }

if !oldRect!.equalTo(newRect!) {
// https://github.com/react-native-video/react-native-video/issues/3085#issuecomment-1557293391
if newRect!.equalTo(UIScreen.main.bounds) {
if newRect!.equalTo(bounds) {
RCTLog("in fullscreen")
if !_fullscreenUncontrolPlayerPresented {
_fullscreenUncontrolPlayerPresented = true
Expand All @@ -1311,7 +1315,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
}
}

self.reactViewController().view.frame = UIScreen.main.bounds
self.reactViewController().view.frame = bounds
self.reactViewController().view.setNeedsLayout()
}
}
Expand Down
9 changes: 7 additions & 2 deletions ios/Video/RCTVideoPlayerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ class RCTVideoPlayerViewController: AVPlayerViewController {
return .portrait
} else {
// default case
let orientation = UIApplication.shared.statusBarOrientation
return orientation
if #available(iOS 13, tvOS 13, *) {
return RCTVideoUtils.getCurrentWindow()?.windowScene?.interfaceOrientation ?? .unknown
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I getter for that would be great!

} else {
#if !os(visionOS)
return UIApplication.shared.statusBarOrientation
#endif
}
}
}

Expand Down
Loading