Search code examples
swiftswift5screen-recording

observe a screen recording in swift 5


I am using the below code to observe when users are recording the screen. the problem is when stop recording and re-record again it won't observe. Any help, please.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    UIScreen.main.addObserver(self, forKeyPath: "captured", options: .new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
    if (keyPath == "captured") {
        let isCaptured = UIScreen.main.isCaptured
        if isCaptured == true {
            black.backgroundColor=UIColor.black
            self.view.addSubview(black)
        } else if isCaptured == false {
            black.isHidden = true
        }
    }
}

Solution

  • I found the solution by applying the same method in viewdidload and that’s it