React-Native/Expo Location status if it user don't want to allow location after giving permission to app
Asked Answered
K

3

7

After allowing permission to access GPS in react native App. If the user rejected to turn on the gps. It will show errors like

Unhandled promise rejection: Error: Location request failed due to unsatisfied device settings."

I want to avoid if the user rejects the Gps turn on option It will return something. so I need If condition for the location whether it is On or Off. (I'm using expo-location)

Kraus answered 23/1, 2020 at 7:4 Comment(2)
sorry couldnt get your doubt, can you specify pleaseHarken
After allowing permission to access GPS in react native App. If the user rejected to turn on the gps .it will shows errors like "Unhandled promise rejection: Error: Location request failed due to unsatisfied device settings." I want to avoid if the user reject the Gps turn on option It will return something. so I need If condition for the location whether it is On or Off. (I'm using expo-location)Kraus
S
8

You're seeing this error because the Location.getCurrentPositionAsync is async method and it returns a promise and if it fails it throws an error (the error you're seeing).

You could wrap your code inside a try and catch block to catch the error and do something about it. Example:

_getLocationAsync = async () => {
    let { status } = await Permissions.askAsync(Permissions.LOCATION);
    if (status !== 'granted') {
      alert('The request was denied');
    }else{
      try{
        let location = await Location.getCurrentPositionAsync({});
        // do something with location
      }catch(e){
        alert('We could not find your position. Please make sure your location service provider is on');
        console.log('Error while trying to get location: ', e);
      }
    }
  }

// call this._getLocationAsync();
Saltzman answered 25/3, 2020 at 18:11 Comment(0)
L
4

you will need to check the status from expo-location and redirect user to settings to allow the permission for that you can use android intents for android and for ios you can use Linking to redirect the user to device settings and give permissions

  requestLocationPermission = async () => {
    const { status } = await Permissions.askAsync(Permissions.LOCATION);    
    if (status === 'granted) {
      navigation.navigate('screen_name');
    } else {
      // Alert Message if user does not allow permissions
      Alert.alert("alert Message", "Instructions based on OS", [
        {
          text: 'Open Settings',
          onPress: () => goToSettings(),
          style: 'cancel',
        },
        { text: Languages.DENY, onPress: () => navigation.goback()},
      ]);
    }
  };

go to settings

  goToSettings = () => {
    if (Platform.OS == 'ios') {
      // Linking for iOS
      Linking.openURL('app-settings:');
    } else {
      // IntentLauncher for Android
      IntentLauncher.startActivityAsync(
        IntentLauncher.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS
      );
    }
  };

  • NOTE Intent launcher is a separate package for android
Lamia answered 23/1, 2020 at 7:43 Comment(2)
Thanks but I need rejection condition (for returning null status)Kraus
permission status does not return null > Permission status with possible values: granted, denied, undetermined. the above code will handle for null value also if not add another else if conditionLamia
W
0

use this to turn gps on:

Location.enableNetworkProviderAsync()

It's a simple popup. (without redirection to a setting)

Wikiup answered 23/3, 2021 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.