Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 288
React Bala Avatar asked Jan 23 '20 07:01

React Bala


People also ask

How do I ask for location permissions in Expo?

ACCESS_COARSE_LOCATION , ACCESS_FINE_LOCATION , and FOREGROUND_SERVICE permissions are automatically added. In order to use background location features, you also must add the android. permission. ACCESS_BACKGROUND_LOCATION and submit your app for review and request access to use the background location permission.

How do I add permissions in React Native Expo?

Refer to this list of all Android permissions and configuration for more information. To use only the minimum necessary permissions that React Native requires to run, set "permissions" : [] . To use those in addition to CAMERA permission, for example, you'd set "permissions" : ["CAMERA"] .


2 Answers

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();
like image 193
Nasser Abachi Avatar answered Jan 02 '23 11:01

Nasser Abachi


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
like image 29
Rizwan Ahmed Shivalli Avatar answered Jan 02 '23 10:01

Rizwan Ahmed Shivalli