|
@@ -12,6 +12,7 @@ import {
|
12
|
12
|
View,
|
13
|
13
|
Alert,
|
14
|
14
|
AppState,
|
|
15
|
+ Platform,
|
15
|
16
|
} from 'react-native';
|
16
|
17
|
|
17
|
18
|
import Permissions from 'react-native-permissions'
|
|
@@ -42,11 +43,24 @@ export default class Example extends Component {
|
42
|
43
|
|
43
|
44
|
_updatePermissions(types) {
|
44
|
45
|
Permissions.checkMultiplePermissions(types)
|
|
46
|
+ .then(status => {
|
|
47
|
+ if (this.state.isAlways) {
|
|
48
|
+ return Permissions.getPermissionStatus('location', 'always')
|
|
49
|
+ .then(location => ({...status, location}))
|
|
50
|
+ }
|
|
51
|
+ return status
|
|
52
|
+ })
|
45
|
53
|
.then(status => this.setState({ status }))
|
46
|
54
|
}
|
47
|
55
|
|
48
|
56
|
_requestPermission(permission) {
|
49
|
|
- Permissions.requestPermission(permission)
|
|
57
|
+ var options
|
|
58
|
+
|
|
59
|
+ if (permission == 'location') {
|
|
60
|
+ options = this.state.isAlways ? 'always' : 'whenInUse'
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ Permissions.requestPermission(permission, options)
|
50
|
64
|
.then(res => {
|
51
|
65
|
this.setState({
|
52
|
66
|
status: {...this.state.status, [permission]: res}
|
|
@@ -64,9 +78,15 @@ export default class Example extends Component {
|
64
|
78
|
}).catch(e => console.warn(e))
|
65
|
79
|
}
|
66
|
80
|
|
|
81
|
+ _onLocationSwitchChange() {
|
|
82
|
+ this.setState({ isAlways: !this.state.isAlways })
|
|
83
|
+ this._updatePermissions(this.state.types)
|
|
84
|
+ }
|
|
85
|
+
|
67
|
86
|
render() {
|
68
|
87
|
return (
|
69
|
88
|
<View style={styles.container}>
|
|
89
|
+
|
70
|
90
|
{this.state.types.map(p => (
|
71
|
91
|
<TouchableHighlight
|
72
|
92
|
style={[styles.button, styles[this.state.status[p]]]}
|
|
@@ -74,7 +94,7 @@ export default class Example extends Component {
|
74
|
94
|
onPress={this._requestPermission.bind(this, p)}>
|
75
|
95
|
<View>
|
76
|
96
|
<Text style={styles.text}>
|
77
|
|
- {p}
|
|
97
|
+ {Platform.OS == 'ios' && p == 'location' ? `location ${this.state.isAlways ? 'always' : 'whenInUse'}` : p}
|
78
|
98
|
</Text>
|
79
|
99
|
<Text style={styles.subtext}>
|
80
|
100
|
{this.state.status[p]}
|
|
@@ -83,13 +103,23 @@ export default class Example extends Component {
|
83
|
103
|
</TouchableHighlight>
|
84
|
104
|
)
|
85
|
105
|
)}
|
86
|
|
- <TouchableHighlight
|
87
|
|
- style={styles.openSettings}
|
88
|
|
- onPress={Permissions.openSettings}>
|
89
|
|
- <Text style={styles.text}>Open settings</Text>
|
90
|
|
- </TouchableHighlight>
|
|
106
|
+ <View style={styles.footer}>
|
|
107
|
+ <TouchableHighlight
|
|
108
|
+ style={styles['footer_'+Platform.OS]}
|
|
109
|
+ onPress={this._onLocationSwitchChange.bind(this)}>
|
|
110
|
+ <Text style={styles.text}>Toggle location type</Text>
|
|
111
|
+ </TouchableHighlight>
|
91
|
112
|
|
92
|
|
- <Text>Note: microphone permissions may not work on iOS simulator. Also, toggling permissions from the settings menu may cause the app to crash. This is normal on iOS. Google "ios crash permission change"</Text>
|
|
113
|
+ <TouchableHighlight
|
|
114
|
+ onPress={Permissions.openSettings}>
|
|
115
|
+ <Text style={styles.text}>Open settings</Text>
|
|
116
|
+ </TouchableHighlight>
|
|
117
|
+ </View>
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+ <Text style={styles['footer_'+Platform.OS]}>
|
|
121
|
+ Note: microphone permissions may not work on iOS simulator. Also, toggling permissions from the settings menu may cause the app to crash. This is normal on iOS. Google "ios crash permission change"
|
|
122
|
+ </Text>
|
93
|
123
|
</View>
|
94
|
124
|
);
|
95
|
125
|
}
|
|
@@ -130,8 +160,13 @@ const styles = StyleSheet.create({
|
130
|
160
|
restricted: {
|
131
|
161
|
backgroundColor: '#FFAB91'
|
132
|
162
|
},
|
133
|
|
- openSettings: {
|
|
163
|
+ footer: {
|
134
|
164
|
padding: 10,
|
135
|
|
- alignSelf: 'flex-end',
|
|
165
|
+ flexDirection: 'row',
|
|
166
|
+ justifyContent: 'space-between',
|
|
167
|
+ },
|
|
168
|
+ footer_android: {
|
|
169
|
+ height: 0,
|
|
170
|
+ width: 0,
|
136
|
171
|
}
|
137
|
172
|
})
|