Show Menu

Do you have a job opening?

Submit your 30 day Job Listing for FREE

Detect screenshots like snapchat logo

Have you got the Snapchat App? Even taken a screenshot of the image because you wanted to keep it forever? Ever wondered how to do that in your own app?

Before iOS 7, detecting screenshots in your application used to be touchesCancelled:withEvent:, however Apple has made it so touchesCancelled:withEvent: is no longer called when the user takes a screenshot with your application open.

This would effectively break Snapchat entirely, so a couple betas in a new solution was added. Now, the solution to detect screenshots in your application is as simple as using NSNotificationCenter to add an observer to UIApplicationUserDidTakeScreenshotNotification.

Here’s some examples of detecting screenshots Objective-C and Swift:

How to detect screenshots in Objective C


NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationUserDidTakeScreenshotNotification
              object:nil
               queue:mainQueue
          usingBlock:^(NSNotification *note) {
             // executes after screenshot
          }];
          
 

How to detect screenshots in Swift


let mainQueue = NSOperationQueue.mainQueue()
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationUserDidTakeScreenshotNotification,
    object: nil,
    queue: mainQueue) { notification in
        // executes after screenshot
}

There you go, now you can detect screenshots in Objective-C and Swift. If you have used this method in one of your apps, post it below in the comments, if you have another way around then please feel to comment and help make this a really good article and help a budding iOS developer with how to detect screenshots in Objective-C and Swift.

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


Written by:

I work in the shadows. You can thank me by being a good soul.

Comments

comments