Sunday, January 18, 2015

iOS Tip - Detecting Screen Capture

In iOS 7 onwards, you can programmatically detect if the user captures the screen using the Home and Lock buttons to take a screen shot. When the user takes a screen capture, the OS fires the UIApplicationUserDidTakeScreenshotNotification notification. Thus, all you have to do is to handle register for this notification and do whenever you want to do when this notification is fired. The following shows an example:

    override func viewDidLoad() {
        super.viewDidLoad()
        
        NSNotificationCenter.defaultCenter().addObserverForName(
            UIApplicationUserDidTakeScreenshotNotification,
            object: nil,
            queue: NSOperationQueue.mainQueue())
            {
                notification in
                println("Screen shot taken!")
            }

    }

Note that this notification is fired only after the screen shot is taken. 

No comments: