androidiosreactjsreact-nativescreenshot

Disable Screen Capture/ScreenShot in React Native App


I have came across few solutions specific for ios and Android to prevent screen-capturing and taking screenshots. But how do i disable screen-capturing in react native?


Solution

  • Android

    Inside /android/app/src/main/java/com/{Project_Name}/MainActivity.java

    you may add the following lines. Prevent capture screen by setFlag FLAG_SECURE, use code below as an example:

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

    later when you want to remove secure flag

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
    

    iOS

    overlay screen in AppDelegate.m, take this example:

    - (void)applicationWillResignActive:(UIApplication *)application {    
        // fill screen with our own colour
        UIView *colourView = [[UIView alloc]initWithFrame:self.window.frame];
        colourView.backgroundColor = [UIColor whiteColor];
        colourView.tag = 1234;
        colourView.alpha = 0;
        [self.window addSubview:colourView];
        [self.window bringSubviewToFront:colourView];
        // fade in the view
        [UIView animateWithDuration:0.5 animations:^{
            colourView.alpha = 1;
        }];
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application {
        // grab a reference to our coloured view
        UIView *colourView = [self.window viewWithTag:1234];
        // fade away colour view from main view
        [UIView animateWithDuration:0.5 animations:^{
            colourView.alpha = 0;
        } completion:^(BOOL finished) {
            // remove when finished fading
            [colourView removeFromSuperview];
        }];
    }