If the app uses a AVPictureInPictureController, the topmost method does not return the right UIViewController. When the video starts playing the AVPictureInPictureController creates two extra UIWindow.
var rootViewController: UIViewController?
let currentWindows = UIApplication.shared.windows
for window in currentWindows {
if let windowRootViewController = window.rootViewController {
rootViewController = windowRootViewController
break
}
}
The first UIWindow is not the key window, it is created by the Picture in Picture View Controller.
If the app uses a AVPictureInPictureController, the topmost method does not return the right UIViewController. When the video starts playing the AVPictureInPictureController creates two extra UIWindow.
var rootViewController: UIViewController? let currentWindows = UIApplication.shared.windows
The first UIWindow is not the key window, it is created by the Picture in Picture View Controller.