DEV Community

Opeyemi Emmanuel Pcharles
Opeyemi Emmanuel Pcharles

Posted on

How to Disable Screenshot Capture in Your React Native Expo App

Privacy and security are critical for mobile applications, developers often need to ensure that sensitive information displayed in their apps cannot be easily captured through screenshots or screen recordings. While React Native Expo provides a powerful framework for building cross-platform apps, it doesn’t offer a built-in way to disable screenshot capture. However, you can achieve this by integrating native code into your Expo project.

In this article, we’ll walk you through the steps to disable screenshot capture in your React Native Expo app for both Android

Prerequisites
Before starting, ensure you have the following:

Expo CLI installed and configured.
A basic understanding of React Native and Expo.
Familiarity with integrating native modules into an Expo project.
Why Disable Screenshot Capture?

Before diving into the technical details, let’s understand why you might want to disable screenshot capture in your app:

Security: Apps that display sensitive information, such as financial details, passwords, or personal data, should prevent unauthorized screenshots to avoid data leaks.
User Privacy: Disabling screenshots can prevent unintended sharing of private content by users.

Step 1: Ejecting from Expo Managed Workflow
Expo’s managed workflow doesn’t support adding custom native code, so you’ll need to eject your Expo project to use the bare workflow. Here’s how you can do it:

Run the following command in your project directory:
expo eject

  1. Follow the prompts to configure your iOS and Android projects. This will generate the native iOS and Android directories required to add custom native code.

Step 2: Implementing Native Code for Android
To disable screenshot capture in Android, you need to modify the MainActivity.java file.

Open android/app/src/main/java/com/yourprojectname/MainActivity.java.
Add the following code inside the onCreate method:

import android.view.WindowManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Prevent screenshots in the app
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
                         WindowManager.LayoutParams.FLAG_SECURE);
}
Enter fullscreen mode Exit fullscreen mode

This code sets the FLAG_SECURE flag on the window, preventing screenshots and screen recording within the app.

Step 3: Testing Your Implementation
After implementing the native code, it’s essential to test the functionality:

On Android, try taking a screenshot within the app; it should be blocked, and the user should receive a message indicating that screenshots are not allowed.

Conclusion
Disabling screenshot capture in your React Native Expo app is crucial for protecting sensitive information. While Expo’s managed workflow doesn’t natively support this feature, by ejecting your app and adding custom native code, you can easily prevent screenshots and screen recordings.

Keep in mind that ejecting from Expo’s managed workflow adds complexity to your project.

Top comments (0)