Created
July 17, 2020 08:48
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@alexdmotoc this might help : https://stackoverflow.com/questions/68421711/how-to-hide-views-from-screenshots-screen-recordings-in-swiftui/77841290#77841290