swiftiframeavplayerwkwebviewwkwebviewconfiguration

Detect when, iframe(AVPlayer) of video player inside wkwebkit


When I am open any website with movie inside WKWebKit and press on players this movie will be open inside some player, where I can pause, remove etc. My question How I can detect when this iframe(window or player, I don't know how it is named) is open or closed and do something in background if window open or closed.

For clarity I am attached screenshot of simulator where this player was opened if I am press on player with movie on website.player


Solution

  • I am solved this problem using NotificationCenter using UIWindowDidBecomeVisibleNotification & UIWindowDidBecomeHiddenNotification.

    My code:

    override func viewDidLoad() {
            super.viewDidLoad()
    
    // listen for videos playing in fullscreen
            NotificationCenter.default.addObserver(self, selector: #selector(onDidEnterFullscreen(_:)), name: UIWindow.didBecomeVisibleNotification, object: view.window)
    
            // listen for videos stopping to play in fullscreen
        NotificationCenter.default.addObserver(self, selector: #selector(onDidLeaveFullscreen(_:)), name: UIWindow.didBecomeHiddenNotification, object: view.window)
    }
    
    @objc func onDidEnterFullscreen(_ notification: Notification) {
        print("Enter Fullscreen")
    }
    
    @objc func onDidLeaveFullscreen(_ notification: Notification) {
        print("Leave Fullscreen")
    }