Swift version: 5.10
In some cases, especially for apps that handle sensitive user data, it’s important to prevent screenshots and screen recordings to enhance security. Apple provides ways to block these actions to protect confidential information.
To get started, you need to modify your app’s behavior using UIScreen and UIApplication.
Method 1: Prevent Screenshots and Screen Recording Using Secure Layer
One of the simplest ways to block screenshots and recordings is by overlaying a secure view when the app is active. This is done by setting a secure window flag in your main app window.
Copied!class SecureViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Create a secure view let secureView = UIView(frame: view.bounds) secureView.backgroundColor = .black secureView.isHidden = true // Add it to the main view view.addSubview(secureView) NotificationCenter.default.addObserver(self, selector: #selector(protectScreen), name: UIApplication.userDidTakeScreenshotNotification, object: nil) } @objc func protectScreen() { let alert = UIAlertController(title: "Warning", message: "Screenshot detected. For security reasons, screenshots are not allowed.", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default)) present(alert, animated: true) } }
How it works:
• The UIApplication.userDidTakeScreenshotNotification observer detects when the user takes a screenshot and alerts them.
• A hidden black overlay (secureView) can be modified to display when needed.
Method 2: Prevent Screen Recording with isCaptured Detection
Another method is using UIScreen.main.isCaptured, which detects if the screen is being recorded.
Copied!class SecurityManager { static let shared = SecurityManager() func monitorScreenRecording() { NotificationCenter.default.addObserver(self, selector: #selector(screenRecordingChanged), name: UIScreen.capturedDidChangeNotification, object: nil) } @objc func screenRecordingChanged() { if UIScreen.main.isCaptured { print("Screen recording detected!") // You can take action, such as logging out the user or displaying a warning screen } } }
How it works:
• UIScreen.main.isCaptured checks if screen recording is active.
• UIScreen.capturedDidChangeNotification listens for changes in the recording state.
• If recording is detected, you can log out the user, blur the screen, or display a warning.
Bonus: Apply a Privacy Screen When App Goes to Background
If your app handles sensitive data, you may also want to hide content when the app moves to the background.
Copied!func applicationDidEnterBackground(_ application: UIApplication) { let privacyView = UIView(frame: UIScreen.main.bounds) privacyView.backgroundColor = .black privacyView.tag = 999 // Unique identifier for removal UIApplication.shared.windows.first?.addSubview(privacyView) } func applicationWillEnterForeground(_ application: UIApplication) { UIApplication.shared.windows.first?.viewWithTag(999)?.removeFromSuperview() }
How it works:
• Adds a black overlay when the app is in the background to prevent unauthorized screen peeks.
• Removes the overlay when the app returns to the foreground.
Conclusion
By implementing these techniques, you can enhance the security of your iOS app:
• Prevent screenshots with userDidTakeScreenshotNotification
• Detect screen recording with isCaptured
• Hide content when the app is in the background