ios - 从 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)
}
}
解决方案
我认为你可以通过制作一个 BaseViewController 来实现这一点,并且所有其他 View Controller 都应该由 BaseViewController 继承,所以你只需要观察 BaseViewController 中的屏幕截图检测,你不必在每个 ViewController 上编写代码
推荐阅读
- stm32 - stm32 spi全双工从机模式
- jquery - 如果下拉菜单有更改,如何重置或未选中的单选按钮
- apache-kafka - 为什么kafka logs dir和zookeeper数据都是空的?
- django - 将芹菜任务重定向到其他队列但不使用它
- firebase - React Native 中的 Firebase Rest API 身份验证 ID 令牌和令牌刷新
- django - 如何修复 django 不一致的日期字段?
- javascript - 如果在 mongodb 中不匹配,如何删除空文档?
- python - 为什么我不能等待 gdal 模块中的 readasarray 方法?
- c++ - 递归反向索引 std::array
- django - 我在使用 Celery、Redis 和 Django 时遇到问题