Prevent Screen Capture in iOS App

Have you ever wondered how we can prevent screenshots from iPhone apps? Well, you are in the right document and we will be discussing the same. 

Apple is not providing any kind of feature that you can enable or disable the screen capture option from the Settings app. But we can customise our code to get the specific result.

Let’s get started

Preventing screen capture in an iOS app is not directly supported by the iOS operating system. Apple’s design philosophy emphasizes the user’s control over their own device, so preventing screen capture goes against those principles. 

Here are a few reasons why app developers might consider implementing measures to prevent screen capturing:

  1. Protecting User Privacy: Some apps handle personal or sensitive data such as financial information, health records, or private messages. Preventing screen capturing can help ensure that this information remains within the app’s secure environment and is not easily shared or exposed.

  2. Intellectual Property Protection: If your app contains proprietary algorithms, unique designs, or valuable content, preventing screen capturing can help safeguard your intellectual property from unauthorised distribution or reproduction.

  3. Compliance with Regulations: Certain industries, such as healthcare, finance, or legal sectors, are subject to strict regulatory requirements regarding data privacy and confidentiality. Preventing screen capturing can assist in meeting these compliance standards and avoiding potential legal or regulatory issues.

  4. Preventing Unauthorised Sharing: By disabling screen capture, you can discourage users from capturing and sharing sensitive content without permission, thereby reducing the risk of information leakage or misuse.

  5. Enhancing Security: Screen capturing can be used as a means to conduct phishing attacks, gather personal information, or exploit vulnerabilities in an app. By preventing screen capturing, you can add an additional layer of security to your app and reduce the risk of unauthorised access or exploitation.

While preventing screen capturing may provide additional protection, it is important to carefully assess the requirements of your app and strike a balance between security measures and user experience. It’s crucial to consider the potential impact on usability, user expectations, and the specific needs of your app before implementing such restrictions.

However, we can do that using customisation of code.

test

In the above code the makeSecure() function is an extension method for the UIView class in Swift. This function aims to add a secure text field as a subview to the current view, making it appear as if the entire view is secure or hidden.

Here’s a breakdown of what the function does:

  1. DispatchQueue.main.async: The function is executed asynchronously on the main queue to ensure UI updates occur on the main thread.

  2. let field = UITextField(): A new instance of UITextField is created. UITextField is a class that provides a text input field in which users can enter text.

  3. field.isSecureTextEntry = true: The isSecureTextEntry property of the text field is set to true. This property masks the entered text, typically used for password or sensitive information entry.

  4. self.addSubview(field): The text field is added as a subview to the current view. By adding it as a subview, the text field will be displayed within the boundaries of the current view.

  5. field.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true and field.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true: Auto Layout constraints are applied to position the text field at the center of the current view vertically and horizontally.

  6. self.layer.superlayer?.addSublayer(field.layer): The layer of the text field is added as a sublayer to the super-layer of the current view's layer. This step attempts to visually overlay the text field's layer on top of the current view's layer.

  7. field.layer.sublayers?.first?.addSublayer(self.layer): The layer of the current view is added as a sublayer to the first sublayer of the text field's layer. This step aims to visually embed the current view within the text field's layer.

The intention behind this implementation is to create a secure or hidden view by overlaying the secure text field on top of the current view’s layer. However, it’s important to note that this implementation is a foolproof method to prevent screen capture content or hide the code of the view.

Usage

One way we can take one UIView in the storyboard. Inside that view we can add our content controls like UIButton, UILabel, UIImageView and etc.

test

Take an @IBOutlet weak var viSecure: UIView! in your UIViewController class and call the function viSecure.makeSecure() .

Result

test

Recorded Video from iPhone

As we can see in the resulting video I've started screen recording from my iPhone but due to our customisation of code video is not able to capture anything. Also capture one screenshot but there is no content captured by it. 

Alerting User When Capturing Screenshot

If we want to alert the user when they capture a screenshot within your iOS app, you can leverage the UIApplicationUserDidTakeScreenshot notification introduced in iOS 11. This notification is triggered when a screenshot is captured by the user. Here's an example of how you can use it to display an alert:

test

In the above example, we register for the UIApplicationUserDidTakeScreenshot notification in the viewDidLoad() method. When the notification is received, the screenshotTaken() method is called. Inside this method, an alert controller is created with a title and message indicating that screenshots are not allowed. The alert is then presented to the user.

Make sure to remove the observer in the deinit() method of your view controller to avoid memory leaks.

Please note that while this method can inform the user about the screenshot capture, it does not prevent the screenshot from being taken.

test

Conclusion

We can’t restrict ourselves from capturing screenshots or recording video but we can hide our content.

Gautam Sareriya

Sr. Software Engineer - iOS

Published: Jun 13, 20233 min read

AWS Certified Team

Tech Holding Team is a AWS Certified & validates cloud expertise to help professionals highlight in-demand skills and organizations build effective, innovative teams for cloud initiatives using AWS.

By using this site, you agree to thePrivacy Policy.