首页 > 解决方案 > 在 React Native App 中禁用屏幕捕获/ScreenShot

问题描述

我遇到了一些专门针对 ios 和 Android 的解决方案,以防止屏幕捕获和截屏。但是如何在本机反应中禁用屏幕捕获?

标签: androidiosreactjsreact-nativescreenshot

解决方案


安卓

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

您可以添加以下行。通过 setFlag 防止捕获屏幕FLAG_SECURE,使用下面的代码作为示例:

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);
}

稍后当您要删除安全标志时

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

iOS

中的覆盖屏幕AppDelegate.m,举个例子:

- (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];
    }];
}

推荐阅读