17

Please use Swift 4+

NOTE: I am detecting the screenshot while I am within the iMessage extension, not in the standard iMessage view.

Update - I came up with a working solution that checks the photo library during the sensitive information period every .3 seconds or so to check if a new screenshot has been added. If the user does not give permission to the photo library, it won't show them the content until they enable it. However, I am still looking for other creative solutions that don't necessarily involve such a tedious process.

I have an iMessage extension and I am trying to detect screenshots. I have tried every observer I have found online and for some reason it is not registering screenshots.

ViewWillAppear()

UIScreen.main.addObserver(self, forKeyPath: "captured", options: .new, context: nil)

Observer

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
    if (keyPath == "captured") {
        let isCaptured = UIScreen.main.isCaptured
        print(isCaptured)
        screenshot()
        //screenshot() sends a message alerting the message was screens hotted. However, the print statement didn't even run.
    }
}

ViewWillDisappear()

UIScreen.main.removeObserver(self, forKeyPath: "captured", context: nil)

I have also tried the standard default Notification Center

let mainQueue = OperationQueue.main
    NotificationCenter.default.addObserver(forName: UIApplication.userDidTakeScreenshotNotification, object: nil, queue: mainQueue) { notification in
        // executes after screenshot
        print("Screenshotted")
        self.screenshot()
    }

For people who claim it is not possible to detect screenshots within an iMessage extension because it is an extension and not a full app, this developer has been able to successfully do it Working Example

Levi K
  • 573
  • 1
  • 4
  • 23

1 Answers1

1

Perhaps it little bit of overkill, but you can transform your image in DRM protected video and system prevent any screenshots / screen sharing / screen recording of DRM protected videos.

ManWithBear
  • 2,787
  • 15
  • 27
  • How do I do this? I'm not sure I understand exactly what you are saying – Levi K Mar 23 '19 at 20:16
  • [FairPlay](https://en.wikipedia.org/wiki/FairPlay) is drm technology to protect your video content from being stolen (Widevine another example). iOS on system level don't allow make screenshots / videos of such content (in case of screenshot here only black screen instead). So you can present protected video instead of image – ManWithBear Mar 23 '19 at 21:58
  • This is may be really heavy if you just want to know if screenshot have been taken. But if you want to completely prevent them this is probably best approach – ManWithBear Mar 23 '19 at 21:59
  • That seems like a somewhat good idea, the only problem is I am presenting text, not photos. So that might be a little overkill trying to turn text into a still video lol – Levi K Mar 23 '19 at 22:12
  • @LeviK text is bunch of pixels, you know :D – ManWithBear Mar 23 '19 at 22:12
  • I know :) but I think the solution I came up with will work for now and I might try implementing a more complex solution like that later on. Thanks for the idea! – Levi K Mar 23 '19 at 22:14