【问题标题】:Checking for permissions always returns "denied". React Native检查权限总是返回“拒绝”。反应原生
【发布时间】:2020-11-04 00:27:09
【问题描述】:

在物理设备 (Android) 上的 React Native 应用程序调试过程中,当我检查位置权限时,它总是被阻止,即使我在设置中授予了权限。我必须注意,我之前无法请求“请求许可”窗口,所以我无法以任何方式阻止它。另外,我尝试删除并让应用程序重新安装。

这是我检查位置权限的代码(我也尝试过其他人)。我在那里使用react-native-permissions,但是如果我使用react-native 中的PermissionsAndroid,行为是相同的。

import {check, PERMISSIONS, request, RESULTS, openSettings} from "react-native-permissions";

async function checkPermissions() {
    let response = await check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION); // <-- always blocked
    let isPermissionsGranted = false;

    if (response === RESULTS.GRANTED) {
        isPermissionsGranted = true;
    } else if (response === RESULTS.DENIED) {
        response = request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION, {
            title: "FreeLine requires permission",
            message: "FreeLine needs access to your location so you can see your position",
            buttonPositive: "Ok",
            buttonNegative: "Don't show my position",
        });

        if (response === RESULTS.GRANTED) {
            isPermissionsGranted = true;
        } else if (response === RESULTS.DENIED) {
            await openSettings();
        }
    }

    return isPermissionsGranted;
}

我没有找到任何可以解释这一点的信息。我认为可能在调试期间我无法请求许可。

【问题讨论】:

  • 可以手动设置权限吗?
  • @Style-7 是的,我可以在设置中更改权限,但这不会改变行为。
  • 某些设备没有 FINE_LOCATION
  • @Style-7 如果该功能不可用,它将返回UNAVAILABLE。此外,我尝试了相机许可,结果是一样的,但我当然有相机。我使用最新版本的 React Native 创建了新项目,现在我正在尝试找出导致该行为的原因。

标签: android react-native location android-permissions


【解决方案1】:

最后,我找到了问题的根源。最有趣的是它与我的代码没有任何联系。问题是由清单引起的,更准确地说是我包含的规则。

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-feature android:name="android.permission.BLUETOOTH" android:required="true"/>
<!-- Only foreground ble access -->
<uses-feature android:name="android.permission.ACCESS_FINE_LOCATION" android:required="true"/>

从上面的 sn-p 可以看出,我使用了use-feature。以下是文档所说的:

Google Play 使用您的应用清单中声明的​​元素从不满足其硬件和软件功能要求的设备中过滤您的应用。

除了上面的规则,我还要加上uses-permission,像这样:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    相关资源
    最近更新 更多