title: iOS Swift cannot dismiss avplayer modal
body: <p>so here is my use case - we are loading a video using avplayer and playing it, and user can click on the default fullscreen button to take the video fullscreen. The user may be viewing the video in 2 different conditions, if he is logged in and if he is not logged in.</p>
  
  <p>If the user is logged in (determined by a variables value), he can watch full video, otherwise, playback should stop after playing a defined number of seconds (depending on the video the no. of seconds changes), and a banner comes up over the player asking him to login.</p>
  
  <p>Everything is working fine while the video is being played inline. However, when the video is playing fullscreen, then even if we stop playback using <code>didTapPause()</code>, the fullscreen window does not get dismissed. I have even tried dismissing it using <code>self.playerController.dismiss(animated: true, completion: nil)</code>, the fullscreen modal is not dismissed. The code snippet it as follows -</p>
  
  <pre><code>playerController.player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main) { (CMTime) -&gt; Void in
          if self.playerController.player?.currentItem?.status == .readyToPlay {
              self.videoCurrentTimeDuration = CMTimeGetSeconds((self.playerController.player?.currentItem!.currentTime())!);
              self.videoTimeDuration = CMTimeGetSeconds((self.playerController.player?.currentItem?.duration)!);
  
  
  
              if self.moveToTime != nil{
                  let timeWithSecond = CMTimeMakeWithSeconds(self.videoTimeDuration! * self.moveToTime! / 100, Int32(kCMTimeMaxTimescale))
  
                  self.playerController.player?.seek(to: timeWithSecond, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
                  self.moveToTime = nil
              }
  
              guard let videoD = self.videoData else { return }
  
              let timeTToPlay: Double = Double(videoD.freeDuration)
  
              if videoD.isFree {
                  if videoD.registrationNeeded &amp;&amp; !CurrentLoginUser.shared.isLogin{
                      if self.videoCurrentTimeDuration! &gt; timeTToPlay {
                          self.didTapPause()
  
                          self.playerController.dismiss(animated: true, completion: nil)
  
  
                          self.loginNeedView = UINib.get(withNib: self)
                          self.loginNeedView?.frame = self.bounds
  
                          self.loginNeedView?.autoresizingMask = [
                              UIViewAutoresizing.flexibleWidth,
                              UIViewAutoresizing.flexibleHeight
                          ]
                          self.addSubview(self.loginNeedView!)
                          AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait)
                      }
                  }
                  else{
                      self.loginNeedView?.removeFromSuperview()
                      AppUtility.lockOrientation(UIInterfaceOrientationMask.all)
                  }
              }
  </code></pre>
  
  <p>The player controller is added onto the view by calling the <code>setupView</code> function which is as follows -</p>
  
  <pre><code>private func setUpView() {
      self.backgroundColor = .black
      addVideoPlayerView()
      configurateControls()
  }
  
  fileprivate func addVideoPlayerView() {
      playerController.view.frame = self.bounds
      playerController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
      playerController.showsPlaybackControls = true
      playerController.addObserver(self, forKeyPath: "videoBounds", options: NSKeyValueObservingOptions.new, context: nil)
      self.insertSubview(playerController.view, at: 0)
  }
  </code></pre>
  
  <p>I am not sure if this is the proper way to do it, any ideas ?</p>
  
  <p>EDIT:</p>
  
  <p>Next as per anbu.karthik's advice I tried to forcibly remove the fullscreen view by locating the top view controller as :</p>
  
  <pre><code>func currentTopViewController() -&gt; UIViewController {
  var topVC: UIViewController? = UIApplication.shared.delegate?.window??.rootViewController
  while ((topVC?.presentedViewController) != nil) {
  topVC = topVC?.presentedViewController
  }
  return topVC! 
  }
  </code></pre>
  
  <p>and then, used it as follows-</p>
  
  <pre><code>let currentTopVC: UIViewController? = self.currentTopViewController()
  
                          if (currentTopVC?.description.contains("AVFullScreenViewController"))! {
                              print("found it")
  
                              currentTopVC?.dismiss(animated: true) { _ in }
                          }
  </code></pre>
  
  <p>And it works but crashes the app with the following exception - </p>
  
  <pre><code>Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:&lt;AVEmbeddedPlaybackControlsViewController: 0x7fdcc2264400&gt; should have parent view controller:&lt;AVPlayerViewController: 0x7fdcc222a800&gt; but actual parent is:&lt;AVFullScreenViewController: 0x7fdcce2f3c50&gt;'
  </code></pre>