8

I am making a React Native app which has a secure section where the user has to enter his password to unlock the protected content. The problem is, when the user switches to another app before locking that section, a screen capture along with the unlocked content will be generated and displayed on the Recents Screen.

Is there a way to replace the preview with something else, a blank screen or a custom screen like the Chrome/Firefox incognito mode?

Blazing Fast
  • 703
  • 1
  • 7
  • 12
  • 2
    In standard Android, you would [use `FLAG_SECURE`](https://stackoverflow.com/a/9822607/115145). I am not certain what the React Native equivalent is. – CommonsWare May 16 '18 at 20:30
  • 1
    Agree with @CommonsWare, i guess there is no proper way. however in android there is `android:excludeFromRecents`. – Masoom Badi May 16 '18 at 21:17
  • 1
    That's unfortunate... Thanks for pointing out the native solutions guys. I found a [FLAG_SECURE native module](https://github.com/kristiansorens/react-native-flag-secure-android) for React Native, will test it out later. Would be great [if there's an equivalent of FLAG_SECURE on iOS](https://stackoverflow.com/questions/46221825/how-to-prevent-users-taking-screenshots-of-app-on-ios). – Blazing Fast May 17 '18 at 05:36

2 Answers2

2

You could listen to the app going in background and show a white/empty modal on top of the current screen

import {AppState} from 'react-native'

class App extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      appState: AppState.currentState
    }
  }

  componentDidMount() {
    AppState.addEventListener("change", this._handleAppStateChange);
  }

  componentWillUnmount() {
    AppState.removeEventListener("change", this._handleAppStateChange);
  }

  _handleAppStateChange = (newAppState) => {
    if(newAppState === 'background' && newAppState !== this.state.appState) {
      // SHOW EMPTY SCREEN
    }

    if(newAppState !== 'background' && this.state.appState === 'background') {
      // HIDE EMPTY SCREEN
    }
    
    if (newAppState !== this.state.appState) {
      this.setState({ appState: newAppState })
    }
  }

  ... rest of the app ....
}

AppState documentation

gbalduzzi
  • 9,356
  • 28
  • 58
0

you can use react-native-flag-secure-android package

yarn add react-native-flag-secure-android

USAGE

import FlagSecure from 'react-native-flag-secure-android';
 
function flagSecure(enable) {
  if (enable) {
    FlagSecure.activate();
  } else {
    FlagSecure.deactivate();
  }
}

Note: it will work for android

Muhammad Numan
  • 23,222
  • 6
  • 63
  • 80