首页 > 解决方案 > Ask for location permission again after user says no the first time

问题描述

I'm looking at the documentation for expo, but it seems their example only triggers a dialog to ask for location permissions once. What can I do to trigger the dialog again if the user says no? All I get is this in the console on repeat attempts to launch the dialog:

Possible Unhandled Promise Rejection (id: 0):
Error: Location permission not granted

Here's my code:

class PermissionsScreen extends React.Component {
  constructor(props) {
    super(props);
  }

    async getLocationAsync() {
        const { Location, Permissions } = Expo;
        const { status } = await Permissions.askAsync(Permissions.LOCATION);
        if (status === 'granted') {
            return Location.getCurrentPositionAsync({enableHighAccuracy: true});
        } else {
            throw new Error('Location permission not granted');
        }
    }

  render() {
    let s = styles;

    return (
      <View style={s.contain}>
        <Text>I'm going to ask for permissions</Text>
        <TouchableOpacity onPress={() => {
            this.getLocationAsync();
        }}>
          <View style={s.button}>
            <Text style={s.buttonText}>Got it</Text>
          </View>
        </TouchableOpacity>
      </View>
    );
  }
}

标签: androidiosreact-nativemobileexpo

解决方案


你在 sdk 29 上有一个独立的应用程序吗?如果是,您可能想看看Expo GitHub 上的这个线程。

此版本存在错误,解决方法如下:

proguard-rules.pro添加这个

-keepclassmembers class * {
  @expo.core.interfaces.ExpoProp *;
}
-keepclassmembers class * {
  @expo.core.interfaces.ExpoMethod *;
}

-keepclassmembers class * {
  @**.expo.core.interfaces.ExpoProp *;
}
-keepclassmembers class * {
  @**.expo.core.interfaces.ExpoMethod *;
}

那么,在app/build.gradle

buildTypes {
  // ...

  release {
    minifyEnabled true
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    // ...
  }
}

推荐阅读