在 swift 5 中观察屏幕录像

observe a screen recording in swift 5

我正在使用下面的代码来观察用户何时录制屏幕。问题是当停止录制并再次重新录制时它不会观察到。请帮忙。

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
        }
    }
}

我通过在 viewdidload 中应用相同的方法找到了解决方案,仅此而已