【问题标题】:Expo Background Permissions Async is not workingExpo后台权限异步不起作用
【发布时间】:2021-09-13 02:33:32
【问题描述】:

我正在开发一个应用程序,有时需要跟踪我的用户,因为他们在应用程序之外。我浏览了世博会文档,看来我应该使用前台和后台权限异步函数,但我的世博会应用程序无法识别任何一个版本。有人遇到过这种情况么?我该如何解决?以下是我的一些代码:

import * as  Permissions from 'expo-permissions'; 
import * as Request from 'expo-permissions';
import * as Location from 'expo-location';
import Constants from 'expo-constants'


useEffect(() => {
    (async () => {
      if (Platform.OS === 'android' && !Constants.isDevice) {
        setErrorMsg(
          'Oops, this will not work on Snack in an Android emulator. Try it on your device!'
        );
        return;
      }
      let { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== 'granted') {
        setErrorMsg('Permission to access location was denied');
        return;
      }
      let location = await Location.getCurrentPositionAsync({});
      setLocation(location);
    })();
  }, []);

  useEffect (() =>{
    (async () => {
      if (Platform.OS === 'android' && !Constants.isDevice){
        setErrorMsg2(
          'Whoops'
        );
        return;
      }
      let { status2 } = await Location.requestBackgroundPermissionsAsync();
      if (status2 !== 'granted') {
        setErrorMsg2('Permission to access background location was denied');
        return;
      }
      let location2 = await Location.getCurrentPositionAsync({})
      setLocation2(location2)
    })()
  },[])

  let text = 'Waiting..';
  if (errorMsg) {
    text = errorMsg;
  }
   else if (location) {
    text = JSON.stringify(location);
  }

  let text2 = 'Also Wating...';
    if (errorMsg2){
      text2 = errorMsg2;
    }
    else if (location) {
      text2 = JSON.stringify(location)
    }

我在托管工作流中运行 Expo,目前使用 Expo 3.23.2 版本,但也在 4.3.2 版本上对其进行了测试,并且遇到了同样的错误。

【问题讨论】:

    标签: javascript react-native permissions background expo


    【解决方案1】:

    在使用expo-location 时,您必须牢记以下几点:

    1. Location.requestBackgroundPermissionsAsync() 在应用处于后台时要求用户授予位置权限。在 Android 11 或更高版本上:此方法将打开系统设置页面 - 在此之前,您应该向用户解释为什么您的应用需要后台位置权限

    2. 申请后台权限前应先授予前台权限(没有前台权限,您的应用无法获得后台权限)

    参考文档here

    注意:这已经在 SDK 41 和 expo-location 的版本 12.2.1 上测试过

    Working Example for Background Location here

    我是如何实现的

    import React, { useState, useEffect } from 'react';
    import { Platform, Text, View, StyleSheet } from 'react-native';
    import * as Location from 'expo-location';
    
    export default function App() {
      const [location, setLocation] = React.useState(null);
      const [errorMsg, setErrorMsg] = React.useState(null);
    
      React.useEffect(() => {
        (async () => {
          let { status } = await Location.requestForegroundPermissionsAsync();
          if (status !== 'granted') {
            setErrorMsg('Permission to access location was denied');
            return;
          }
    
          let location = await Location.getCurrentPositionAsync({});
          setLocation(location);
    
          let backPerm = await Location.requestBackgroundPermissionsAsync();
          console.log(backPerm);
        })();
      }, []);
    
      let text = 'Waiting..';
      if (errorMsg) {
        text = errorMsg;
      } else if (location) {
        text = JSON.stringify(location);
      }
    
      return (
        <View style={styles.container}>
          <Text style={styles.paragraph}>{text}</Text>
        </View>
      );
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        paddingTop: 50,
        backgroundColor: '#ecf0f1',
        padding: 8,
      },
      paragraph: {
        margin: 24,
        fontSize: 18,
        fontWeight: 'bold',
        textAlign: 'center',
      },
    });
    

    首先,它询问我前台权限,我点击了While Using the app

    然后,对于后台位置权限,它会将我带到设备设置页面以允许在后台访问位置

    如果万一这没有按预期工作,请尝试以下步骤

    1. 清除授予Expo Go 应用的所有权限。
    2. 清除缓存和应用数据
    3. 卸载并重新启动您的设备,然后再次下载Expo go 应用程序

    【讨论】:

    • 我不断收到同样的错误:“Location.requestForegroundPermissionsAsync 不是函数。”知道为什么会这样吗?
    • 确保您的导入语句正确。像这样import * as Location from 'expo-location'; 而不是这样import { Location } from "expo-location"。另外,我提供的零食在您的设备上运行良好吗?
    • 不,我就是这样,它仍然会引发错误。和我的 Expo 版本有关系吗?
    • 是的,我也这么认为。我提供的零食正在 Expo SDK 41 上工作。未在 SDK 42 上检查。
    • 我目前正在运行 Expo 3.23.2,所以虽然我不知道我的 SDK(我认为它是 40),但我绝对没有最新的大声笑
    猜你喜欢
    • 2020-12-30
    • 2021-09-14
    • 2017-03-29
    • 2021-06-26
    • 2011-05-30
    • 2012-07-03
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多