Monitor device location settings with React Native

885 views Asked by At

Is there a way to monitor when a phone has their location settings (not permissions) turned on or off?

I know there's a way to check with react-native-device-info with the code below, but is there a way to add a listener to constantly check?

DeviceInfo.isLocationEnabled().then((enabled) => {
  // true or false
});
1

There are 1 answers

1
k0uh0t On

When users change location setting, Users open setting page. It means you should know if app is foreground or background.

In that case, you can use AppState (https://reactnative.dev/docs/appstate).

To monitor when a phone has their location settings (not permissions) turned on or off, You can implement like this.

import {AppState} from 'react-native';

useEffect(() => {
  const subscription = AppState.addEventListener('change', (nextAppState) => {
    // if app is foreground.
    if (nextAppState === 'active') {
      DeviceInfo.isLocationEnabled().then((enabled: boolean) => {
        // true or false
      });
    }
  });
  return () => {
    subscription.remove();
  };
}, []);