博览会定位权限在安卓上可以使用,但在iOS上不行。

4

我有一个使用Expo的React Native应用程序。在Android上,位置权限可以正常工作,但在iOS上不能。在真实的iPhone(iOS版本14.4.2)上,它没有任何反应。在模拟器上,我收到以下错误消息:

[未处理的承诺拒绝:错误:需要LOCATION_FOREGROUND权限执行此操作。] 位于node_modules/react-native/Libraries/BatchedBridge/NativeModules.js:103:50的promiseMethodWrapper 位于node_modules/@unimodules/react-native-adapter/build/NativeModulesProxy.native.js:15:23的moduleName.methodInfo.name 在http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:293333:90中的watchPositionAsync$ 在http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:293327:37中的watchPositionAsync

更新:我在另一台安卓设备上尝试了它,但没有成功,所以可能是因为在运行先前版本时已经授予了权限,所以它可以在我的主要安卓手机上工作?关于Expo 40和41版本之间权限出现问题的错误报告https://github.com/expo/expo/issues/12581
以下是我正在使用的内容:

app.json:

{
  "expo": {
    "name": "Andiamo",
    "slug": "andiamo_places",
    "version": "1.0.0",
    "orientation": "default",
    "icon": "./assets/icon.png",
    "splash": {
      "image": "./assets/splash.png",
      "resizeMode": "contain",
      "backgroundColor": "#ffffff"
    },
    "updates": {
      "fallbackToCacheTimeout": 0
    },
    "assetBundlePatterns": [
      "**/*"
    ],
    "ios": {
      "bundleIdentifier": "com.andiamoplaces.andiamotravelapp",
      "supportsTablet": true,
      "infoPlist": {
        "NSLocationUsageDescription":
          "The app needs permission to access your location, if you want to be located on the map.",
        "NSLocationWhenInUseUsageDescription": "The app needs permission to access your location, if you want to be located on the map",
        "NSLocationAlwaysUsageDescription": "The app needs permission to access your location, if you want to be located on the map",
        "NSLocationAlwaysAndWhenInUseUsageDescription": "The app needs permission to access your location, if you want to be located on the map",
        "NSPhotoLibraryUsageDescription": "The app needs permission to your camera roll, if you want to upload pictures.",
        "NSCameraUsageDescription": "The app needs permission to your camera, if you want to take pictures from the app."
      }
    },
    "android": {
      "package": "com.andiamoplaces.andiamotravelapp",
      "adaptiveIcon": {
        "foregroundImage": "./assets/adaptive-icon.png",
        "backgroundColor": "#FFFFFF"
      },
      "permissions": [
        "CAMERA",
        "LOCATION"
      ]
    },
    "web": {
      "favicon": "./assets/favicon.png"
    },
    "description": ""
  }
}

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",
    "postinstall": "jetifier -r"
  },
  "dependencies": {
    "@expo/vector-icons": "^12.0.0",
    "@react-native-community/clipboard": "^1.5.1",
    "@react-native-community/datetimepicker": "3.2.0",
    "@react-native-community/masked-view": "0.1.10",
    "@react-navigation/native": "^3.8.4",
    "@use-expo/font": "^2.0.0",
    "cross-fetch": "^3.1.4",
    "expo": "^41.0.0",
    "expo-camera": "~11.0.2",
    "expo-cli": "^4.4.4",
    "expo-clipboard": "~1.0.2",
    "expo-facebook": "~11.0.5",
    "expo-font": "~9.1.0",
    "expo-image-manipulator": "~9.1.0",
    "expo-image-picker": "~10.1.4",
    "expo-location": "~12.0.4",
    "expo-media-library": "~12.0.2",
    "expo-permissions": "~12.0.1",
    "expo-status-bar": "~1.0.4",
    "firebase": "8.2.3",
    "geolib": "^3.3.1",
    "global": "^4.4.0",
    "luxon": "^1.26.0",
    "native-base": "^2.15.2",
    "react": "16.13.1",
    "react-devtools": "^4.13.2",
    "react-dom": "16.13.1",
    "react-native": "https://github.com/expo/react-native/archive/sdk-41.0.0.tar.gz",
    "react-native-color-picker": "^0.6.0",
    "react-native-draggable-flatlist": "^2.6.1",
    "react-native-gesture-handler": "~1.10.2",
    "react-native-ionicons": "^4.6.5",
    "react-native-maps": "0.27.1",
    "react-native-material-menu": "^1.2.0",
    "react-native-modal": "^11.10.0",
    "react-native-pell-rich-editor": "^1.7.0",
    "react-native-popup-menu": "^0.15.10",
    "react-native-prompt-crossplatform": "^1.6.1",
    "react-native-ratings": "^8.0.4",
    "react-native-reanimated": "~2.1.0",
    "react-native-safe-area-context": "3.2.0",
    "react-native-safe-area-view": "^1.1.1",
    "react-native-screens": "~3.0.0",
    "react-native-simple-radio-button": "^2.7.4",
    "react-native-switch": "^2.0.0",
    "react-native-switch-selector": "^2.1.4",
    "react-native-vector-icons": "^8.1.0",
    "react-native-web": "~0.13.12",
    "react-native-webview": "11.2.3",
    "react-native-youtube-iframe": "^2.0.1",
    "react-navigation": "^4.4.4",
    "react-navigation-drawer": "^2.7.1",
    "react-navigation-stack": "^2.10.4",
    "react-navigation-tabs": "^2.11.1",
    "react-redux": "^7.2.4",
    "redux": "^4.1.0",
    "redux-persist": "^6.0.0",
    "redux-thunk": "^2.3.0"
  },
  "devDependencies": {
    "@babel/core": "~7.9.0",
    "jetifier": "^1.6.8"
  },
  "private": true
}

代码:

startUserLocationUpdates = async () => {
    const returns = await Location.watchPositionAsync(
      {
        accuracy: Location.Accuracy.BestForNavigation,
        timeInterval: 1000,
        // distanceInterval: 0,
      },
      (loc) => {
        if (this.props.ShowLocation) this.centerMapTo(loc.coords.latitude, loc.coords.longitude);
        this.props.onSetCurrentLocation({ latitude: loc.coords.latitude, longitude: loc.coords.longitude });
      }
    );
    this._removeLocationUpdates = returns.remove;
  };

它在Android上工作的原因是因为我之前使用了正确的权限代码并在我的Android设备上运行了它并进行了注释。没有意识到权限只会被询问一次。所以我不得不重新安装Expo客户端来验证它在Android上也无法工作。我使用了这个代码:const { status } = await Permissions.askAsync(Permissions.LOCATION_FOREGROUND); if (status === “granted”) {…}这个代码可以工作,但会给出警告消息,表示该代码现已过时,因此我将其更改为:let { status } = await Location.requestForegroundPermissionsAsync(); - Tom
1个回答

1

格式化代码的权限的更新版本。

if (Platform.OS !== "web") {
  const { status } = await Location.requestForegroundPermissionsAsync();
  
  if (status !== "granted") {
    Alert.alert(
      "Insufficient permissions!",
      "Sorry, we need location permissions to make this work!",
      [{ text: "Okay" }]
    );
    return;
  }
}

事实上,我遇到了相反的情况;iOS正常工作,但没有上面的代码,Android无法正常工作。
AndroidManifest.xml中的<uses-permission android:name=...是由Expo自动处理的,因此您不需要关心它并修改任何内容。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接