首页 > 解决方案 > 从 ios 中的单个页面在整个应用程序中进行屏幕截图检测

问题描述

我理解,屏幕截图预防是不可能的,但我们可以像 snapchatdoes 一样做,我们可以检测到它。

我的应用程序由超过 10 个控制器组成,所以每个页面 addobserver 有点乏味,所以如果我可以将它放在 appdelegate/Scenedelegate 或任何其他上,以便在任何控制器屏幕截图上被通知,则需要解决方案。放置是主要需要的东西这里

诸如可达性之类的东西,它以类似的方式用于网络检测

这是代码:

 func detectScreenShot(action: @escaping () -> ()) {
       UIScreen.main.addObserver(self, forKeyPath: "captured", options: .new, context: nil)
        
        let mainQueue = OperationQueue.main
        NotificationCenter.default.addObserver(forName: UIApplication.userDidTakeScreenshotNotification, object: nil, queue: mainQueue) { notification in
            // executes after screenshot
            print(notification)
            action()
        }
    }



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

标签: iosswiftscreenshot

解决方案


我认为你可以通过制作一个 BaseViewController 来实现这一点,并且所有其他 View Controller 都应该由 BaseViewController 继承,所以你只需要观察 BaseViewController 中的屏幕截图检测,你不必在每个 ViewController 上编写代码


推荐阅读