简体   繁体   English

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

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

I'm working on a React Native app built with Expo and am trying to get the location function to work.我正在开发一个使用 Expo 构建的 React Native 应用程序,并试图让定位功能正常工作。 While testing it out using Expo Go, I am asked for permissions to use the location feature, but when I build it out an IOS build and test on testflight, I'm never asked for permission.在使用 Expo Go 对其进行测试时,我被要求获得使用位置功能的权限,但是当我将其构建为 IOS 版本并在 testflight 上进行测试时,我从未被要求获得许可。 I've tried looking in settings to add the permission, but location isn't even an option.我试过在设置中添加权限,但位置甚至不是一个选项。

Where I ask for permission in the app我在应用程序中请求许可的地方

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

IOS section of app.json: 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",          
        ]
      }
    },

package.json:包.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
}

Looking at the anonymous function in your useEffect , it isn't called correctly.查看您的useEffect中的匿名函数,它没有被正确调用。

Anonymous functions need () after them to be called.匿名函数需要在它们之后调用() Example:例子:

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

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

I took a look at Expo's docs as well and I believe what you are trying to accomplish in your useEffect is the following:我还查看了 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

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

Without the () your anonymous function in your useEffect isn't being called, which is probably why you're never asked for permission.如果没有() ,您的useEffect中的匿名函数就不会被调用,这可能就是您从未被请求许可的原因。

As for your app.json and package.json , it looks like you have everything properly installed.至于你的app.jsonpackage.json ,看起来你已经正确安装了所有东西。 What you should make sure is that in the "plugins" section of app.json you have expo-location showing up.您应该确保在app.json"plugins"部分中显示了expo-location

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

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