Real time permission in React Native

纵饮孤独 提交于 2019-12-11 06:36:50

问题


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.


回答1:


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);
    }
  }



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/52701561/real-time-permission-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!