Install
$ yarn add rn-screenshot-detector
React Native >= 0.60.0
pod install inside of the ios sub folder
Edit file AppDelegate.m
#import <RNScreenshotDetector/RNScreenshotDetector.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ... setup code
// Somewhere React Native will have placed something like
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL...]
// Somewhere below this you can setup the screenshot detector to listen for events
RNScreenshotDetector* screenshotDetector = [[RNScreenshotDetector alloc] init];
[screenshotDetector setupAndListen:rootView.bridge];
}React Native < 0.60.0
$ react-native link rn-screenshot-detector
- In XCode, in the project navigator, right click
Libraries➜Add Files to [your project's name] - Go to
node_modules➜react-native-screenshot-detectorand addRnScreenShot.xcodeproj - In XCode, in the project navigator, select your project. Add
libRnScreenShot.ato your project'sBuild Phases➜Link Binary With Libraries - Run your project (
Cmd+R)<
- Open up
android/app/src/main/java/[...]/MainApplication.java
- Add
import com.reactlibrary.RnScreenShotPackage;to the imports at the top of the file - Add
new RnScreenShotPackage()to the list returned by thegetPackages()method
- Append the following lines to
android/settings.gradle:include ':react-native-screenshot-detector' project(':react-native-screenshot-detector').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-screenshot-detector/android') - Insert the following lines inside the dependencies block in
android/app/build.gradle:compile project(':react-native-screenshot-detector')
import { DisableScreenShot, EnableScreenShot } from 'rn-screenshot-detector'
componentDidMount(){
...
DisableScreenShot(this.callBack);
}
componentWillUnmount(){
...
EnableScreenShot()
}
callBack=()=>{
Alert.alert("Warning","It isn't safe to take a screenshot")
}For ios, we can not disable screenshot, so we just detect action screenshot. For android, any action screenshot or recording screen will be return black screen.