Home > Mobile >  get Permission for location on Android with PermissionsAndroid
get Permission for location on Android with PermissionsAndroid

Time:10-08

I am a little bit confused. I am using the example from:

<reactnative.dev/docs/permissionsandroid>

and I am trying to get the permission for the location on android, but it is always saying:

permission is null

Here is my code:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow strict-local
 */
import React, {useEffect} from 'react';
import {
    StatusBar,
    PermissionsAndroid,
    Platform
} from 'react-native';
import Geolocation from '@react-native-community/geolocation';

//import SearchResults from './src/screens/SearchResults';
import DestinationSearch from './src/screens/DestinationSearch';
//import HomeScreen from './src/screens/HomeScreen';

navigator.geolocation = require('@react-native-community/geolocation');

const App: () => React$Node = () => {

  const androidPermission = async() => {
      try {
        const granted = await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.ACCESS_FINA_LOCATION,
          rationale ={
            title: "App location Permission",
            message:
              "App needs access to your location "  
              "so you can take awesome rides.",
            buttonNeutral: "Ask Me Later",
            buttonNegative: "Cancel",
            buttonPositive: "OK"
          }
        );
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
          console.log("You can use the location");
        } else {
          console.log("location permission denied");
        }
      } catch (err) {
        console.warn(err);
      }
    }
  useEffect(create= () => {
        if(Platform.OS ==='android'){
            //request android permission
            androidPermission();
        }else{
            //request IOS permission
            Geolocation.requestAuthorization();
        }
    }, inputs=[])

  return (
    <>
      <StatusBar barStyle= 'dark-content' />
      <DestinationSearch />
    </>
  );
};

export default App;

can anyone help me? I even tryed to give the app the permission in the settings option, but it says still "permission is null" I restartet my android emulator but it didn't change anything.

CodePudding user response:

Please try below code

and do one more thing clean the gradle and re-install the app and

import React, { useEffect, useState, useRef } from 'react'
import { SafeAreaView, Text, PermissionsAndroid } from 'react-native'


export default function Welcome() {

    useEffect(() => {
        setTimeout(() => {
            _checkPermission()
        }, 1000)
    }, [])

    const _checkPermission = async () => {

        try {
            const result = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
            console.log('result', typeof result)
            console.log('result', result)

            if (result == true) {
                // you code
            }
            else if (result == false) {
                const status = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)

                if (status === 'never_ask_again') {
                    // Your code
                }
                else if (status === 'denied') {
                    _checkPermission()
                }
                else if (status === 'granted') {
                    // Your code
                }
            }


        } catch (error) {
            console.log('error', error)
        }
    }
    return (
        <SafeAreaView>
            <Text>
                Welcome.js
            </Text>
        </SafeAreaView>
    )

}
  • Related