繁体   English   中英

IOS上的React Native和Expo位置不要求权限

[英]React Native and Expo location on IOS not asking for permissions

我正在开发一个使用 Expo 构建的 React Native 应用程序,并试图让定位功能正常工作。 在使用 Expo Go 对其进行测试时,我被要求获得使用位置功能的权限,但是当我将其构建为 IOS 版本并在 testflight 上进行测试时,我从未被要求获得许可。 我试过在设置中添加权限,但位置甚至不是一个选项。

我在应用程序中请求许可的地方

useEffect(() => {
        (async () => {
            let { status } = await Location.requestForegroundPermissionsAsync();
            if (status !== 'granted') {
                setErrorMsg('Permission to access location was denied');
                return;
            }
        }, []);
    }, []);

app.json的IOS部分:

"ios": {
      "supportsTablet": true,
      "bundleIdentifier": "com.nawdevelopment.discgolfgames",
      "buildNumber": "1.0.4",
      "infoPlist":{
        "NSLocationUsageDescription":"Disc Golf Games uses location to determine distances, which is used for several games",
        "NSLocationWhenInUseUsageDescription":"Disc Golf Games uses location to determine distances, which is used for several games",
        "NSLocationAlwaysUsageDescription":"Disc Golf Games uses location to determine distances, which is used for several games",
        "NSLocationUsageDescription":"Disc Golf Games uses location to determine distances, which is used for several games",
        "UIBackgroundModes": [
          "location",          
        ]
      }
    },

包.json:

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@react-native-community/masked-view": "^0.1.11",
    "@unimodules/core": "~7.2.0",
    "@unimodules/react-native-adapter": "~6.5.0",
    "ansi-regex": "^4.1.1",
    "expo": "^44.0.0",
    "expo-linear-gradient": "~11.0.3",
    "expo-location": "~14.0.1",
    "expo-permissions": "^13.2.0",
    "expo-status-bar": "~1.2.0",
    "expo-task-manager": "~10.1.0",
    "expo-updates": "~0.11.7",
    "minimist": "^1.2.6",
    "node-fetch": "^2.6.1",
    "plist": "^3.0.5",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-native": "0.64.3",
    "react-native-elements": "^3.4.1",
    "react-native-gesture-handler": "~2.1.0",
    "react-native-reanimated": "~2.3.1",
    "react-native-safe-area-context": "3.3.2",
    "react-native-screens": "~3.10.1",
    "react-native-web": "0.17.1",
    "react-navigation": "^4.4.4",
    "react-navigation-stack": "^2.10.4",
    "react-navigation-tabs": "^2.11.1",
    "unimodules-permissions-interface": "^6.1.0",
    "url-parse": "1.5.10"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9"
  },
  "private": true
}

查看您的useEffect中的匿名函数,它没有被正确调用。

匿名函数需要在它们之后调用() 例子:

(function() {
    console.log('Hi!');
})();

来源: https ://www.javascripttutorial.net/javascript-anonymous-functions/

我还查看了 Expo 的文档,我相信您在useEffect中尝试完成的工作如下:

useEffect(() => {
    (async () => {
      let { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== 'granted') {
        setErrorMsg('Permission to access location was denied');
        return;
      }
    })();
  }, []); //You have this line twice instead and are missing the line above this one

来源: https ://docs.expo.dev/versions/v45.0.0/sdk/location/#usage

如果没有() ,您的useEffect中的匿名函数就不会被调用,这可能就是您从未被请求许可的原因。

至于你的app.jsonpackage.json ,看起来你已经正确安装了所有东西。 您应该确保在app.json"plugins"部分中显示了expo-location

暂无
暂无

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

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