如何检查 iOS11 屏幕录制是打开还是关闭?

Ume*_*ath 9 ios swift ios11

为了检测 iOS11 屏幕录制功能的开或关,我使用了 isCaptured 和 UIScreenCapturedDidChange Notification。

当我第一次启动应用程序并在 iOS11 内置屏幕录制功能上时,它会使用值 True 通知选择器方法,但是当我杀死(终止)正在运行的应用程序并再次启动应用程序时,再次执行相同的过程,然后我的选择器方法是没有被调用。

这是我的代码:

我在 ViewWillAppear() 方法中添加了一个观察者:

NotificationCenter.default.addObserver(self, selector: #selector(handleNotification), name: NSNotification.Name.UIScreenCapturedDidChange, object: nil)
Run Code Online (Sandbox Code Playgroud)

选择器方法如下:

@objc
func handleNotification(notification:Notification){

    let isCaptured = UIScreen.main.isCaptured

    print("isCaptured value = \(isCaptured)")
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我需要终止该应用程序,清除缓存并再次启动该应用程序以获取屏幕录制事件。

请建议我可以在这里做些什么来检测录制事件以保护我的内容不被录制。

dim*_*mdy 13

斯威夫特 4

添加观察者

UIScreen.main.addObserver(self, forKeyPath: "captured", options: .new, context: nil)
Run Code Online (Sandbox Code Playgroud)

接收更改

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
    if (keyPath == "captured") {
        let isCaptured = UIScreen.main.isCaptured

        print(isCaptured)
    }
}
Run Code Online (Sandbox Code Playgroud)


yas*_*urk 3

我想你总是可以检查这个变量,无论通知如何

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let isCaptured = UIScreen.main.isCaptured
    return true
}
Run Code Online (Sandbox Code Playgroud)