React native 获取同一组件中相机和位置的权限似乎无法正常工作?

React native 获取同一组件中相机和位置的权限似乎无法正常工作?,react-native,expo,React Native,Expo,问题: 我在同一个组件中创建了一个react本地应用程序,我访问设备位置和Camara。构建独立应用程序后,请求权限失败。但在我重新启动应用程序后,它工作正常。我不知道这是什么原因 这就是我请求权限的方式 import React, { Component } from "react"; import { View, Text, StyleSheet, TouchableOpacity, Image, TextInput, ScrollView, Keyboar

问题:

我在同一个组件中创建了一个react本地应用程序,我访问设备位置和Camara。构建独立应用程序后,请求权限失败。但在我重新启动应用程序后,它工作正常。我不知道这是什么原因

这就是我请求权限的方式

import React, { Component } from "react";

import {
  View,
  Text,
  StyleSheet,
  TouchableOpacity,
  Image,
  TextInput,
  ScrollView,
  KeyboardAvoidingView,
  Dimensions,
  Button,
  Animated,
  Easing,
  Alert,
  Picker,
  BackHandler,
  ToastAndroid
} from "react-native";
import PageHeader from "../Shared/pageHeader/PageHeader";
import MapView, { PROVIDER_GOOGLE } from "react-native-maps";
import { offenceActions, fineActions } from "../../actions";
import { connect } from "react-redux";
import RenderOption from "./RenderOption";

import * as Permissions from "expo-permissions";

import spinner from "../../../assets/spinner.gif";

const MARGIN = 40;

import { BarCodeScanner } from "expo-barcode-scanner";

class Home extends Component {
  constructor(props) {
    super(props);
    this.state = {
      QrPress: false,
      hasCameraPermission: null,
      // lastScannedUrl: "0x4A014AbE1951E1DaD7c6cf4b629073EebdFeA3e1",
      lastScannedUrl: "",
      scanned: false,
      focusedLocation: {
        latitude: 6.9336686,
        longitude: 79.8489527,
        latitudeDelta: 0.0322,
        longitudeDelta:
          (Dimensions.get("window").width / Dimensions.get("window").height) *
          0.0322
      },
      locationChosen: false,
      placesList: [],
      isFocused: false,
      location: "",
      fineId: "",
      isDateTimePickerVisible: false,
      fineId: "",
      licenceNo: "",
      vehicle: "",
      vehicleNo: "",
      driverDetails: "",
      offenceId: "",
      licenceNoError: "",
      vehicleError: "",
      vehicleNoError: "",
      driverDetailsError: "",
      offenceIdError: "",
      isLoading: false,
      locationError: "" 
    };

    this.randomIdGenerator = this.randomIdGenerator.bind(this);
    this.buttonAnimated = new Animated.Value(0);
    this.growAnimated = new Animated.Value(0);
    this._onPress = this._onPress.bind(this);
    this.clearForm = this.clearForm.bind(this)
  }

  componentDidMount() {

    this.getPermissionsAsync(); 
    this.props.getOffence();
    this.randomIdGenerator();
    navigator.geolocation.getCurrentPosition(
      pos => {
        const coordsEvent = {
          nativeEvent: {
            coordinate: {
              latitude: pos.coords.latitude,
              longitude: pos.coords.longitude
            }
          }
        };

        this.pickLocationHandler(coordsEvent);
      },
      err => {
        console.log(err);
        alert("Fetching the Position failed");
      }
    );
    BackHandler.addEventListener("hardwareBackPress", this.handleBackButton);
  }  


 getPermissionsAsync = async () => {
    const { status } = await Permissions.askAsync(Permissions.CAMERA);
    this.setState({ hasCameraPermission: status === "granted" });
  };

有人能帮我解决这个问题吗?。为了找到解决这个问题的办法,我做了很多努力。但是我无法这样做。

您应该检查权限,并在授予权限后执行任务

getPermissionsAsync = async () => {
  const { status } = await Permissions.askAsync(Permissions.CAMERA);
  if(status === "granted"){
      // use the camera or location service
   }

  };
因为权限可以更改,换句话说,它在今天是动态的。到
保护应用程序成功运行,需要时必须首先检查它

您应该检查权限,并在授予权限后执行任务

getPermissionsAsync = async () => {
  const { status } = await Permissions.askAsync(Permissions.CAMERA);
  if(status === "granted"){
      // use the camera or location service
   }

  };
因为权限可以更改,换句话说,它在今天是动态的。到 已成功保护应用程序运行,需要时必须首先检查它