0

I'm trying to get the user's location in expo in react native. The enableNetworkproviderAsync() prompts a popup which enables the location. I want to re-open the popup when the user declines sharing their location. Currently when I navigate to my map screen the popup opens up only once and when I click on not sharing my location it won't re-open.

const [locationReady, setLocationReady] = useState(false);

  useEffect(() => {
    (async () => {
        if (!locationReady) {
        try {
          const enableLocation = await Location.enableNetworkProviderAsync();
          setLocationReady(true);
        } catch (e) {
          setLocationReady(false);
          console.log("locationReady erorr");
        }
      }
      })();
  }, [locationReady]);

2 Answers 2

1

Once the user declines a permission request, you won't be to ask request it again. You'll have to request user to go to settings and allow the required permission. Two ways you can do this,

  1. import {Linking} from 'react-native'; and then open settings inside your component with Linking.openSettings().
  2. Or you can use the library https://www.npmjs.com/package/react-native-permissions
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, I will try this. If the location is enabled and after some time the user disables the location sharing, how can I prompt an alert to re-enable the location?
You cannot prompt an alert to re-enable location once the user has declined it. iOS simple doesn't allow it. On android , the same happens when user checks "Never ask again" while requesting permission. So once the user has declined the request or disabled it via device settings, you have to ask the user to enable it from settings, instead of the initial alert to enable location(this just wouldnt show up).
But if I re enable the location from the settings, nothing happens. How can I check if it was enabled? I found this method in the docs: hasServicesEnabledAsync() (returns true when the location is enabled). Here is a pastebin: pastebin.com/G3Ta1KrK . I described the problem in the pastebin.
0

You need to check response from enableLocation. Here are the docs.

If the user declines the response has a scope to tell you, what he selected.

But you are setting it to true for every response and the error is never reached. Try this:

 if (!locationReady) {
        try {
          const enableLocation = await Location.enableNetworkProviderAsync();
          setLocationReady(enableLocation.scope !== "none");
        } catch (e) {
          setLocationReady(false);
          console.log("locationReady erorr");
        }

5 Comments

I tried this, still won't reopen after I decline it. I logged the error. It says [Error: Location request failed due to unsatisfied device settings.]
Do you have the google play service installed on your emulator?
I'm testing it on my android phone via USB and expo app.
If you also added the permissions to android manifest, this should work. Can you reproduce that for us in a sandbox?
Well I can try. I looked in to my app.json file and under android there is no permission. Do I need to include that? But if that was not there, how did I get permission for the location? Because if I open the the Map screen and the first time the popup shows, after allowing the location, it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.