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