IOS iMessage extension screenshot detection
Asked Answered
C

1

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

Crowded answered 15/3, 2019 at 3:53 Comment(14)
Your question isn't too clear, how are you planning on detecting a user screenshotted a message without them both having the extension?Modulator
@Modulator It is when the extension is open, not just in the standard iMessage view. I updated the question to be more explicitCrowded
Could you give me details on how you're testing this?Modulator
@Modulator I am using two iPhones that are both running the app, not simulatorsCrowded
maybe this help #13485016Hunterhunting
@antonioyaphiar I have seen that post and tried using the correct answer to no availCrowded
Have you tried, .UIApplicationUserDidTakeScreenshot?Modulator
@Modulator I did UIApplication.userDidTakeScreenshotNotification. Are those the same things? I think it is just the updated versionCrowded
Maybe try to figure out how snapchat did it before IOS7? tmblr.co/Z1ONayZt0qPwModulator
@Modulator okay, I will look into that, not sure if it will work thoughCrowded
I'm sure there's a way to do this but I usually do iOS apps not iMessage so I'm not really that familiar with how you'd do something like this, but wish you the best of luck!Modulator
@Modulator thanks! I couldn't get it to work but hopefully somebody else has a solutionCrowded
@Modulator it would be greatly appreciated if you could upvote so hopefully we can find an answerCrowded
Done that for ya! :)Modulator
Z
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.

Zinnes answered 22/3, 2019 at 18:27 Comment(6)
How do I do this? I'm not sure I understand exactly what you are sayingCrowded
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 imageZinnes
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 approachZinnes
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 lolCrowded
@LeviK text is bunch of pixels, you know :DZinnes
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!Crowded

© 2022 - 2024 — McMap. All rights reserved.