简体   繁体   English

如何在 React Native / Expo 中请求权限?

[英]How to request permissions in React Native / Expo?

I am trying to request a user's location.我正在尝试请求用户的位置。 I tried writing an async function to tell me if my request was processed, but it is ignored.我尝试编写异步 function 来告诉我我的请求是否已处理,但它被忽略了。 I am prompted with a "location request" but I believe it is actually the Expo app and not my function.我收到“位置请求”提示,但我相信它实际上是 Expo 应用程序,而不是我的 function。

Below is some of my code:下面是我的一些代码:

import React, { useState, useEffect, Component }from "react";

import { Permissions , Request } from 'expo-permissions'

//This is the async function I wrote to prompt the user to give permission //这是我写的async function 提示用户给权限

async function getLocationAsync(){
  const { status, permissions } = await Permissions.askAsync( Permissions.LOCATION);
if (status === 'granted'){
console.log('It worked!')
} 
else {
  throw new Error('Location permission not granted');
  }
}

//This logs the terminal and lets me know that the user's current location has been isolated (mounting). //这会记录终端并让我知道用户的当前位置已被隔离(挂载)。 When the app no longer needs their location, it dismounts to prevent a memory leak.当应用不再需要它们的位置时,它会卸载以防止 memory 泄漏。

const Screen = ({navigation})=> { 
    const [user_latitude, setUserLatitude] = useState(0)
    const [user_longitude, setUserLongitude] = useState(0)
    const [position_error, setPositionError] = useState(null)


useFocusEffect( 
      React.useCallback(()=> {
        
      let isActive = true;

      const fetchGeoPosition = () => {
        navigator.geolocation.getCurrentPosition(
        position => { 
          if (isActive){

          setUserLatitude(position.coords.latitude);
          setUserLongitude(position.coords.longitude);
          setPositionError(null);


          console.log('Location Accessed')

          
        } 
        setIsLoading(false)

      }, 

      error => isActive && setPositionError(error.message),
      {enableHighAccuracy: true, timeout: 0, maximumAge: 1000} 
      ); 
    }

    fetchGeoPosition()

      return () =>{
        isActive = false
        console.log('Location Severed')
          }
        }, 
      [],
       ),
    )
    
 

Check this library for Permission on react-native检查此库以获取 react-native 的权限

Here's https://www.npmjs.com/package/react-native-permissions .这是https://www.npmjs.com/package/react-native-permissions

For Android only there a default Package in react-native.对于 Android 仅在 react-native 中存在默认 Package。 ( PermissionAndroid) (权限安卓)

https://reactnative.dev/docs/permissionsandroid https://reactnative.dev/docs/permissionsandroid

Update your manifest file also.同时更新您的清单文件。 Indicating that the application going to use external resource which requires user permission.表示应用程序将使用需要用户权限的外部资源。

https://developer.android.com/guide/topics/manifest/uses-permission-element https://developer.android.com/guide/topics/manifest/uses-permission-element

And For iOS update info.plist file对于 iOS 更新 info.plist 文件

https://www.iosdev.recipes/info-plist/permissions/ https://www.iosdev.recipes/info-plist/permissions/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM