react-nativemobilescreenshot

React Native - expo-screen-capture


I was working with the react native, and was trying to do screen capture restrictions for my app. This is basically kind of an answer for the folks facing this issue.

So, the issue I faced is basically when i applied the usePreventScreenCapture(); inside the function, but what happened was, it got applied to all the routes and screens, so I was not able to take screenshot in any of the pages.


Solution

  • Solution:

    So, the solution i found out is by using useFocusEffect()

        useCallback(() => {
          const preventCapture = async () => {
            try {
              await ScreenCapture.preventScreenCaptureAsync();
            } catch (error) {
              console.warn('Failed to prevent screen capture:', error);
            }
          };
          const allowCapture = async () => {
            try {
              await ScreenCapture.allowScreenCaptureAsync();
            } catch (error) {
              console.warn('Failed to allow screen capture:', error);
            }
          };
          preventCapture();
          return () => {
            allowCapture();
          };
        }, [])
      );
    

    So, by this way, the screen capture prevention happens only that screen during its focus...