user11558118
user11558118

Reputation:

How to prevent screenshots in Android or React-Native app

How do I prevent a user to take screenshots in React-Native app? I have read in some comments on some other posted questions people saying that this line of code will prevent screenshots, but where exactly do I have to insert it?

getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

Or if there is any other way just let me know.

P.S: I am interested to know only for Android platform.

Upvotes: 4

Views: 2750

Answers (1)

Junius L
Junius L

Reputation: 16132

You need to insert it in MainActivity.java, In your MainActivity.java override onCreate method.

package com.reactnativepreventscreenshot;

import android.os.Bundle;
import android.view.WindowManager;

import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "reactnativepreventscreenshot";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
    }
}

DEMO

Upvotes: 4

Related Questions