mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-05 17:30:38 +08:00
Handle "Never Ask Again" in permissions and add requestMultiplePermissions
Summary: In order to get featured in the Google Play Store, we had to handle a few specific cases with permissions based on feedback from the editorial team. First, which was previously possible with this permissions module was bumping the sdk to version 23. The second is requesting multiple permissions at one time. In order for the camera + upload to work, we needed to request both camera permissions + media storage in one flow. The last is handling the case where the user checks the "Never Ask Again" box. This will only appear after a user denies a permission once and is then prompted again. The logic for handling this case is taken from here: http://stackoverflow.com/questions/31928868/how-do-we-distinguish-never-asked-from-stop-asking-in-android-ms-runtime-permis/35495372#35495372 We were also seeing a few crashes similar to #10009 due to `onRequestPermissionsResult` being called before `onResume` (http://stackoverflow.com/questions/35205643/why-is-onresume-called-after-onrequestpermissionsresult), so I delaye Closes https://github.com/facebook/react-native/pull/10221 Differential Revision: D4232551 fbshipit-source-id: fee698d1c48a2d86623cb87996f3d17f4c10a62e
This commit is contained in:
committed by
Facebook Github Bot
parent
5d30045211
commit
51efaab120
@@ -27,13 +27,15 @@ const React = require('react');
|
||||
const ReactNative = require('react-native');
|
||||
const {
|
||||
PermissionsAndroid,
|
||||
Picker,
|
||||
StyleSheet,
|
||||
Text,
|
||||
TextInput,
|
||||
TouchableWithoutFeedback,
|
||||
View,
|
||||
} = ReactNative;
|
||||
|
||||
const Item = Picker.Item;
|
||||
|
||||
exports.displayName = (undefined: ?string);
|
||||
exports.framework = 'React';
|
||||
exports.title = 'PermissionsAndroid';
|
||||
@@ -41,7 +43,7 @@ exports.description = 'Permissions example for API 23+.';
|
||||
|
||||
class PermissionsExample extends React.Component {
|
||||
state = {
|
||||
permission: PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
|
||||
permission: PermissionsAndroid.PERMISSIONS.CAMERA,
|
||||
hasPermission: 'Not Checked',
|
||||
};
|
||||
|
||||
@@ -49,13 +51,14 @@ class PermissionsExample extends React.Component {
|
||||
return (
|
||||
<View style={styles.container}>
|
||||
<Text style={styles.text}>Permission Name:</Text>
|
||||
<TextInput
|
||||
autoFocus={true}
|
||||
autoCorrect={false}
|
||||
style={styles.singleLine}
|
||||
onChange={this._updateText}
|
||||
defaultValue={this.state.permission}
|
||||
/>
|
||||
<Picker
|
||||
style={styles.picker}
|
||||
selectedValue={this.state.permission}
|
||||
onValueChange={this._onSelectPermission.bind(this)}>
|
||||
<Item label={PermissionsAndroid.PERMISSIONS.CAMERA} value={PermissionsAndroid.PERMISSIONS.CAMERA} />
|
||||
<Item label={PermissionsAndroid.PERMISSIONS.READ_CALENDAR} value={PermissionsAndroid.PERMISSIONS.READ_CALENDAR} />
|
||||
<Item label={PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION} value={PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION} />
|
||||
</Picker>
|
||||
<TouchableWithoutFeedback onPress={this._checkPermission}>
|
||||
<View>
|
||||
<Text style={[styles.touchable, styles.text]}>Check Permission</Text>
|
||||
@@ -71,14 +74,14 @@ class PermissionsExample extends React.Component {
|
||||
);
|
||||
}
|
||||
|
||||
_updateText = (event: Object) => {
|
||||
_onSelectPermission = (permission: string) => {
|
||||
this.setState({
|
||||
permission: event.nativeEvent.text,
|
||||
permission: permission,
|
||||
});
|
||||
};
|
||||
|
||||
_checkPermission = async () => {
|
||||
let result = await PermissionsAndroid.checkPermission(this.state.permission);
|
||||
let result = await PermissionsAndroid.check(this.state.permission);
|
||||
this.setState({
|
||||
hasPermission: (result ? 'Granted' : 'Revoked') + ' for ' +
|
||||
this.state.permission,
|
||||
@@ -86,7 +89,7 @@ class PermissionsExample extends React.Component {
|
||||
};
|
||||
|
||||
_requestPermission = async () => {
|
||||
let result = await PermissionsAndroid.requestPermission(
|
||||
let result = await PermissionsAndroid.request(
|
||||
this.state.permission,
|
||||
{
|
||||
title: 'Permission Explanation',
|
||||
@@ -95,8 +98,9 @@ class PermissionsExample extends React.Component {
|
||||
' because of reasons. Please approve.'
|
||||
},
|
||||
);
|
||||
|
||||
this.setState({
|
||||
hasPermission: (result ? 'Granted' : 'Revoked') + ' for ' +
|
||||
hasPermission: result + ' for ' +
|
||||
this.state.permission,
|
||||
});
|
||||
};
|
||||
@@ -125,4 +129,7 @@ var styles = StyleSheet.create({
|
||||
touchable: {
|
||||
color: '#007AFF',
|
||||
},
|
||||
picker: {
|
||||
flex: 1,
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user