检查权限反应本机返回false

时间:2020-06-30 16:07:36

标签: react-native async-await es6-promise

先谢谢了。 我在android-react本机中请求权限检查 最初,我使用checkPermissions检查权限,如果返回true,则继续。 但是,如果其为假,我将请求权限requestPermissions。在请求是否授予双方许可的同时,我在等待false时得到requestPermissions().then(response => {的响应

即使我从requestPermission返回true,为什么我也会得到false作为响应。

 React.useEffect(() => {
        SplashScreen.show();
        checkPermissions().then(async hasPermission => {
          if (hasPermission) {
            console.log('You already have permission');
            // getTokenAndProceed();
          } else {
            await requestPermissions().then(response => {
              console.log(response);
              if (response) {
                console.log('Now permission obtained from user');
              }
            });
          }
        });
    }, []);


     const checkPermissions = async () => {
        if (Platform.OS === 'android' && Platform.Version < 23) {
          return true;
        }
    
        const hasPermission = await PermissionsAndroid.check(
          PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        );
    
        const hasStoragePermission = await PermissionsAndroid.check(
          PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
        );
    
        if (hasPermission && hasStoragePermission) {
          return true;
        } else {
          return false;
        }
      };

const requestPermissions = async () => {
    await PermissionsAndroid.requestMultiple([
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
    ]).then(result => {
      if (
        result['android.permission.ACCESS_FINE_LOCATION'] &&
        result['android.permission.WRITE_EXTERNAL_STORAGE'] ===
          PermissionsAndroid.RESULTS.GRANTED
      ) {
        return true;
      } else if (
        result['android.permission.ACCESS_FINE_LOCATION'] ||
        result['android.permission.WRITE_EXTERNAL_STORAGE'] ===
          PermissionsAndroid.RESULTS.DENIED
      ) {
        console.log('Permission Not Granted');
        return false;
      }
    });
  };

0 个答案:

没有答案