Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save righettod/8f115d0916eff11a5345dc6cfce04a43 to your computer and use it in GitHub Desktop.
Save righettod/8f115d0916eff11a5345dc6cfce04a43 to your computer and use it in GitHub Desktop.
Code to detect when a user perform a screen capture or screen recording of an application in order to prevent it when possible
import UIKit
//Inspired from the code below:
//https://github.com/takashings/ScreenCapturedSample/blob/master/ScreenCapturedSample/ForScreenCapturedViewController.swift
//https://www.hackingwithswift.com/example-code/uikit/how-to-detect-when-the-user-takes-a-screenshot
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//Define a listener to handle the case when a screen recording is launched
//for example using the iPhone built-in feature
NotificationCenter.default.addObserver(
self,
selector: #selector(didScreenRecording(_:)),
name: UIScreen.capturedDidChangeNotification,
object: nil
)
//Define a listener to handle the case when a screenshot is performed
//Unfortunately screenshot cannot be prevented but just detected...
NotificationCenter.default.addObserver(
self,
selector: #selector(didScreenshot(_:)),
name: UIApplication.userDidTakeScreenshotNotification,
object: nil)
return true
}
@objc private func didScreenshot(_ notification: Notification) {
#if DEBUG
//Never add this log in RELEASED app.
print("Screen capture detected then we force the immediate exit of the app!")
#endif
//Information about the image is not available here and screenshot cannot be prevented AS IS
//See hint here about a way to address this issue:
//https://tumblr.jeremyjohnstone.com/post/38503925370/how-to-detect-screenshots-on-ios-like-snapchat
//In all case: Send notification to the backend and clear all local data/secret from the storage/keychain
exit(0)
}
@objc private func didScreenRecording(_ notification: Notification) {
//If a screen recording operation is pending then we close the application
print(UIScreen.main.isCaptured)
if UIScreen.main.isCaptured {
#if DEBUG
//Never add this log in RELEASED app.
print("Screen recording detected then we force the immediate exit of the app!")
#endif
exit(0)
}
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
@alexdmotoc
Copy link

Any idea how one would implement the notes in https://tumblr.jeremyjohnstone.com/post/38503925370/how-to-detect-screenshots-on-ios-like-snapchat ?

I want to achieve the same effect that WhatsApp has for View Once photos, which is to add an overlay over sensitive content when the user takes a screenshot

IMG_4176

@righettod
Copy link
Author

Hi,

Honestly I do not know how to achieve such behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment