I am asking for location permission for my Android React Native app. I am using the npm package: react-native-permissions. I have created a custom hook to do this.

My implementation for iOS works perfect. While trying a similar approach on Android, the dialog that asks the user for location permission never pops up. On my initial check for the permission, my app reports that permission is already granted!! But how?

I include this in my AndroidManifest.xml: <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

and this is my hook that asks for permission:

export default (setPermissionsGranted = () => {}, setPermissionError = () => {}) => {
   const appState = useRef(AppState.currentState)
   const [shouldCheckPermission, setShoudCheckPermission] = useState(false)

   useEffect(() => {
      let isMounted = true

      const handleAppState = (nextAppState) => {
         if (appState.current.match(/inactive|background/) && nextAppState === 'active') {
            // the app has come into the foreground. Change a boolean state variable to trigger
            // handlePermission() again.
            if (isMounted) setShoudCheckPermission(!shouldCheckPermission)
         }
         appState.current = nextAppState
      }

      const handlePermissionStatus = (result) => {
         const status = {
            unavailable: () => {},
            denied: async () => {
               const res = await request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION)
               if (isMounted) {
                  if (res === RESULTS.GRANTED) {
                     setPermissionsGranted(true)
                  } else {
                     setPermissionsGranted(false)
                  }
               }
            },
            limited: () => {
               // todo find out what limited entails
               if (isMounted) setPermissionsGranted(true)
            },
            granted: async () => {
               if (isMounted) setPermissionsGranted(true)
            },
            blocked: () => {
               if (isMounted) setPermissionsGranted(false)
            },
         }
         return status[result]
      }

      AppState.addEventListener('change', handleAppState)

      const handlePermission = async () => {
         console.log('permissions code is run')
         try {
            const res = await check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION)
            console.log('res', res)
            handlePermissionStatus(res)()
         } catch (err) {
            throw err
         }
      }

      handlePermission().catch((e) => {
         if (isMounted) setPermissionError(e.message)
      })

      return () => {
         AppState.removeEventListener('change', handleAppState)
         isMounted = false
      }
   }, [shouldCheckPermission])
}

Any explanation as to why the user never get's asked and the permission is automatically granted?

Another peculiarity, I have commented out all of the code that requests permission for Location on Android, restarted the metro server, uninstalled the app and then re-installed it. The request-permissions tag is still in the AndroidManifest.xml. Apparently that's all I needed to do and now permissions are AUTOMATICALLY granted!

My understanding is that this is a dangerous permission and should not be granted automatically but Android is treating it as a safe-permission. ‍♂️

Thanks in advance.

0

There are 0 answers