原始React:permission always return 'never ask again'

svujldwt  于 2023-04-12  发布在  React
关注(0)|答案(4)|浏览(426)

我正在使用以下代码库请求权限,但它总是返回“never ask again”

async requestPermission(request){
        try{
           const response= await PermissionsAndroid.request('PermissionsAndroid.PERMISSIONS.CAMERA',{
            'title': 'Cool Photo App Camera Permission',
            'message': 'Cool Photo App needs access to your camera ' +
                       'so you can take awesome pictures.'
          })
           console.log(response)
        }catch(err){

        }
        this.getcurrentLocation()
    }

//响应never_ask_again

f1tvaqid

f1tvaqid1#

我最近遇到了这个关于LOCATION服务权限的问题。我忘记在AndroidManifest.xml中添加android.permission.ACCESS_FINE_LOCATION。
所以,我想你也忘了加上
<uses-permission android:name="android.permission.CAMERA" />
在AndroidManifest.xml中

6jjcrrmo

6jjcrrmo2#

我做了几次清理和重建,最后开始工作,不知何故,构建缓存没有正确更新。

acruukt9

acruukt93#

从AndroidManifest.xml中删除tools:node="remove"修复了我的问题。将<uses-permission tools:node="remove" android:name="android.permission.CAMERA"/>替换为<uses-permission android:name="android.permission.CAMERA"/>
https://github.com/zoontek/react-native-permissions/issues/504#issuecomment-914417383

xdnvmnnf

xdnvmnnf4#

我的建议,以处理这种情况是打开设置和直接用户直接适应的权利,见下面的例子:

import { Linking } from 'react-native';
import { Alert } from 'react-native';

const openSettings = () => {
  Linking.openSettings();
};

const askForPermission = async () => {
  try {
    const result = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
      {
        title: 'Storage Permission',
        message: 'App needs access to your storage to read files',
        buttonPositive: 'OK',
        buttonNegative: 'Cancel',
      },
    );
    if (result === PermissionsAndroid.RESULTS.GRANTED) {
      console.log('Storage Permission Granted.');
    } else if (result === PermissionsAndroid.RESULTS.DENIED) {
      console.log('Storage Permission Denied.');
    } else if (result === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
      console.log('Storage Permission Denied with Never Ask Again.');
      Alert.alert(
        'Storage Permission Required',
        'App needs access to your storage to read files. Please go to app settings and grant permission.',
        [
          { text: 'Cancel', style: 'cancel' },
          { text: 'Open Settings', onPress: openSettings },
        ],
      );
    }
  } catch (err) {
    console.log(err);
  }
};

相关问题