Aditya
Aditya

Reputation: 53

Real time permission in React Native

I am new to react native . I want to ask real time permission for Location .Please help me how to take Real Time Permission in react native.

Upvotes: 2

Views: 1392

Answers (4)

Sébastien Mascha
Sébastien Mascha

Reputation: 144

Be carefull geolocation is depreciated now. You better use Amal' solution.

import {geolocation } from "react-native";

Upvotes: 0

Amal p
Amal p

Reputation: 3052

Best avoid libraries for permission check

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Important - include the above code line in your manifest file

 import { PermissionsAndroid, Platform } from 'react-native';

  componentDidMount() {
       if (Platform.OS === 'android') {
              this.requestLocationPermission(); // function call
         }
   }

Below code contains function body.

  async requestLocationPermission() {
     try {
       const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED || granted === true) 
       {
          // WHAT TODO WHEN PERMISSION IS GRANTED
       } else {
         // WHAT TODO WHEN PERMISSION IS NOT GRANTED
      }
    } catch (err) {
      log(err);
    }
  }

Upvotes: 0

Vinzzz
Vinzzz

Reputation: 11724

react-native-permissions might be overkill if you only need access to location. React Native seems to have its own API for that : RN geolocation

import {geolocation } from "react-native";

// ...

geolocation.requestAuthorization()

Permission required on iOS depends on which keys you have in your plist. For android, I guess you have nothing to do

Upvotes: 0

yangguang1029
yangguang1029

Reputation: 1823

take a look at this library: react-native-permissions

Upvotes: 1

Related Questions