React Native-授予权限后切换到其他屏幕

时间:2019-07-18 15:08:44

标签: javascript android reactjs react-native android-permissions

所以我有2个屏幕,即SplashScreen.js和Auth.js。在SplashScreen.js中,所有的Android权限都在运行,如果被授予,它将导航到AuthScreen.js。

但就我而言,在授予应用程序后崩溃。我只是不知道为什么。

所以我想发生的是,在授予Android权限后,它将导航到AuthScreen.js。

这是我的代码:

SplashScreen.js:位于class SplashScreen extendes Component {}

内部
constructor(props) {
super(props);

this.state = {
  timePassed: false
};
}

componentDidMount() {
this.permissionAndroid();
};

setTimePassed() {
this.setState({timePassed: true})
};

permissionAndroid = () => {
try {
  const granted = PermissionsAndroid.requestMultiple(
    [PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE],
    {
      title: "Location Permission",
      message: "You have to, because it's a party app! " + "So you can enjoy more viewing events.",
      buttonNeutral: "Ask me later",
      buttonNegative: "Cancel",
      buttonPositive: "OK"
    },
  );
  if (granted == PermissionsAndroid.RESULTS.GRANTED) {
    console.log("All permision enabledenabled")
    // this.setTimePassed();
    this.setState({timePassed: true})
  } else {
    console.log('Location Permission Denied');
  }
} catch (err) {
  console.warn(err);
}
}

render() {
if(!this.state.timePassed) {
  return (
    <View style={styles.container}>
      <Text>Hello</Text>
    </View>
  );
} else {
  return <AuthScreen/>
}
};

我还使用async function在类外使用了Android权限代码,我现在不使用它,因为this.setState({})或函数setTimePassed()无法正常工作,因为在课外。

这是使应用程序正常工作的另一种选择,但是我不确定setState({})setTimePassed()在类之外传递时将如何工作。

此代码也位于SplashScreen.js中:(它与功能permissionAndroid相同,只是具有异步功能)

async function requestLocationPermission() {
try {
const granted = await PermissionsAndroid.requestMultiple(
  [PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE],
  {
    title: "Location Permission",
    message: "You have to, because it's a party app! " + "So you can enjoy more viewing events.",
    buttonNeutral: "Ask me later",
    buttonNegative: "Cancel",
    buttonPositive: "OK"
  },
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
  console.log("All permision enabledenabled")
  // setTimePassed();
  this.setState({
    timePassed: true
  });
} else {
  console.log('Location Permission Denied');
}
} catch (err) {
  console.warn(err);
}
}

2 个答案:

答案 0 :(得分:1)

您应该首先将permissionAndroid标记为异步,因为它是异步函数,并使用await。我检查了React Native docs,他们说PermissionsAndroid.requestMultiple()

“提示用户在同一对话框中启用多个权限,并返回一个对象,该对象的权限为键,字符串为值(请参见上面的结果字符串),指示用户是允许还是拒绝该请求或不希望被询问再次。”

因此,此函数不会返回类似于boolean的{​​{1}},而是返回具有以下结构的PermissionsAndroid.request()

array

对于const granted = PermissionsAndroid.requestMultiple( [PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE], { title: "Location Permission", message: "You have to, because it's a party app! " + "So you can enjoy more viewing events.", buttonNeutral: "Ask me later", buttonNegative: "Cancel", buttonPositive: "OK" } ); console.log(granted); // { PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION : true, PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE : false } 来说,您是对的:您不能在requestLocationPermission类之外调用setState。您可以使用回调作为参数,在其中您将对两种情况(允许和拒绝)执行必需的操作。或者,您可以简单地将代码移到类中。

@dimonD是正确的,您应该使用诸如React Navigation或React Native Navigation之类的导航框架。

答案 1 :(得分:0)

我认为您需要使用一些导航,例如reactnavigation.org。 授予用户权限后,您只需

navigation.navigate(“您要显示的屏幕”)

这也是身份验证流程的一个概念,您可以在其中实现用户获得LoginScreen之前/之后应该发生的事情。
你可以在这里检查 https://reactnavigation.org/docs/en/auth-flow.html

相关问题