michalchudziak / react-native-geolocation

Geolocation APIs for React Native
MIT License
1.33k stars 234 forks source link

Is there a way of getting result of permission request made by getCurrentPosition or requestAuthorization? (IOS) #142

Open ardatumay opened 3 years ago

ardatumay commented 3 years ago

Hello, When app is first installed and location permissions is not given by the user, calling getCurrentPosition first requests permission from user and then, without waiting result of user permission, returns the position. However, if user gives permission little bit late which is later than return of this method, this method returns error as current position. But user already give the permission. So I need to wait for user to finished her/his interaction with permission dialog. How to do this?

Another problem: When requestPermission method is called, it requests a permission but never returns the result of permission dialog when user presses one of the options. How to know whether user give the permission or not?

All questions is for ios.

Thanks for any suggestion.

longb1997 commented 3 years ago

+1

ioitiki commented 3 years ago

+1

androidfanatic commented 3 years ago

+1

ioitiki commented 3 years ago

@androidfanatic

I was able to solve this using the more recently maintained drop in replacement for this package.

'react-native-geolocation-service'

hope this helps :D

androidfanatic commented 3 years ago

@ioitiki I did the exact same thing after struggling for an hour! Cheers.

Link for the lazy: https://github.com/Agontuk/react-native-geolocation-service

stealkiller06 commented 3 weeks ago

If you don't want to change the library, you can check the permission using react-native-permissions

export const requestAuthorization = async (): Promise<boolean> => {
  const permission =
    Platform.OS === 'android'
      ? PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION
      : PERMISSIONS.IOS.LOCATION_WHEN_IN_USE;

  try {
    const result = await check(permission);
    if (result === RESULTS.GRANTED) {
      return true;
    } else if (result === RESULTS.DENIED || result === RESULTS.BLOCKED) {
      const requestResult = await request(permission);
      return requestResult === RESULTS.GRANTED;
    } else {
      return false;
    }
  } catch (err) {
    console.warn(err);
    return false;
  }
};