スクリーンショットを撮ったことを検知する
iOSでユーザーがスクリーンショットを撮ったタイミングで何かするときのメモ。
スクリーンショットの画像にアクセスできるとかではなく、単に スクリーンショットを撮った
ことを検知するだけなので要注意です。
例えばQRコードやワンタイムパスワードなどを表示していて、スクリーンショットを撮ったら値を変えたい場合にこの方法が役に立ちます。
方法
UIApplicationUserDidTakeScreenshot
の通知を受け取ることでスクリーンショットを撮ったことを検知することができます。
override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(ViewController.didTakeScreenshot(notification:)), name: .UIApplicationUserDidTakeScreenshot, object: nil) } deinit { NotificationCenter.default.removeObserver(self) } @objc func didTakeScreenshot(notification: Notification) { print("didTakeScreenshot") }