michalchudziak / react-native-geolocation

Geolocation APIs for React Native
MIT License
1.31k stars 228 forks source link

Android App crashes when I change Location permission from settings react native #226

Open amir-ibrahim opened 1 year ago

amir-ibrahim commented 1 year ago

I have a problem with React Native Permissions on Android. If the user changes Location Permission from the Device setting while the app is running the app crashes right after that.

I search on the internet for a while I found someone who said this is a normal android behavior to restart the app after permissions changes but there is some app out there that manages to avoid this.

did anyone out there know how to handle this?

ask the user to allow Location permission code:

useEffect(() => {
    Geolocation.getCurrentPosition(
      ({coords}) => {
        let position = {
          latitude: coords.latitude,
          longitude: coords.longitude,
          latitudeDelta: latitudeDelta,
          longitudeDelta: longitudeDelta,
        };
        setLocation(position);
      },
      error => {
        if (enabled && Platform.OS === 'android') {
          return false;
        } else {
          Alert.alert(t('Access Denied'), error.message);
        }
      },
      {
        enableHighAccuracy: false,
        timeout: 10000,
        maximumAge: 0,
      },
    );
  }, []);
michalchudziak commented 1 year ago

Do you have a crash stack trace? I'll take a look on that

Mujahid122418 commented 1 year ago

I have a problem with my app when revoking permissions from settings, the system kills the app. Immediately. while the app is running

Apps like Uber/Snapchat are able to manage this scenario. I have checked some other apps as well which are able to check dynamic changes from settings.

did anyone out there know how to handle this?

const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, { title: 'Location Access Required', message: 'This App needs to Access your location', }, ); if (granted) { console.log('granted ==>', granted); } else { console.log('denied'); }

BraveEvidence commented 1 year ago

This will help https://www.youtube.com/watch?v=E7X9DjezSR0