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

React Native and Expo location on IOS not asking for permissions

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

我在应用中请求权限的地方

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

IOS app.json 部分:

"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:

{
  "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