Skip to content

Commit d0d0c18

Browse files
author
Yonah Forst
committed
update example and readme for pr #74 (Allow location type to be specified in getPermissionStatus)
1 parent 09b14d4 commit d0d0c18

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

Example/Example.js

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
View,
1313
Alert,
1414
AppState,
15+
Platform,
1516
} from 'react-native';
1617

1718
import Permissions from 'react-native-permissions'
@@ -42,11 +43,24 @@ export default class Example extends Component {
4243

4344
_updatePermissions(types) {
4445
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+
})
4553
.then(status => this.setState({ status }))
4654
}
4755

4856
_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)
5064
.then(res => {
5165
this.setState({
5266
status: {...this.state.status, [permission]: res}
@@ -64,17 +78,23 @@ export default class Example extends Component {
6478
}).catch(e => console.warn(e))
6579
}
6680

81+
_onLocationSwitchChange() {
82+
this.setState({ isAlways: !this.state.isAlways })
83+
this._updatePermissions(this.state.types)
84+
}
85+
6786
render() {
6887
return (
6988
<View style={styles.container}>
89+
7090
{this.state.types.map(p => (
7191
<TouchableHighlight
7292
style={[styles.button, styles[this.state.status[p]]]}
7393
key={p}
7494
onPress={this._requestPermission.bind(this, p)}>
7595
<View>
7696
<Text style={styles.text}>
77-
{p}
97+
{Platform.OS == 'ios' && p == 'location' ? `location ${this.state.isAlways ? 'always' : 'whenInUse'}` : p}
7898
</Text>
7999
<Text style={styles.subtext}>
80100
{this.state.status[p]}
@@ -83,13 +103,23 @@ export default class Example extends Component {
83103
</TouchableHighlight>
84104
)
85105
)}
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>
91112

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>
93123
</View>
94124
);
95125
}
@@ -130,8 +160,13 @@ const styles = StyleSheet.create({
130160
restricted: {
131161
backgroundColor: '#FFAB91'
132162
},
133-
openSettings: {
163+
footer: {
134164
padding: 10,
135-
alignSelf: 'flex-end',
165+
flexDirection: 'row',
166+
justifyContent: 'space-between',
167+
},
168+
footer_android: {
169+
height: 0,
170+
width: 0,
136171
}
137172
})

Example/ios/Example/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
<string>test</string>
4242
<key>NSContactsUsageDescription</key>
4343
<string>test</string>
44+
<key>NSLocationAlwaysUsageDescription</key>
45+
<string>test</string>
4446
<key>NSLocationWhenInUseUsageDescription</key>
4547
<string>test</string>
4648
<key>NSMicrophoneUsageDescription</key>

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Promises resolve into one of these statuses
113113
###Methods
114114
| Method Name | Arguments | Notes
115115
|---|---|---|
116-
| `getPermissionStatus` | `type` | - Returns a promise with the permission status. Note: for type `location`, iOS `AuthorizedAlways` and `AuthorizedWhenInUse` both return `authorized` |
116+
| `getPermissionStatus` | `type` | - Returns a promise with the permission status. See iOS Notes for special cases |
117117
| `requestPermission` | `type` | - Accepts any permission type except `backgroundRefresh`. If the current status is `undetermined`, shows the permission dialog and returns a promise with the resulting status. Otherwise, immediately return a promise with the current status. See iOS Notes for special cases|
118118
| `checkMultiplePermissions` | `[types]` | - Accepts an array of permission types and returns a promise with an object mapping permission types to statuses |
119119
| `getPermissionTypes` | *none* | - Returns an array of valid permission types |
@@ -123,11 +123,17 @@ Promises resolve into one of these statuses
123123
###iOS Notes
124124
Permission type `bluetooth` represents the status of the `CBPeripheralManager`. Don't use this if only need `CBCentralManager`
125125

126-
`requestPermission` also accepts a second parameter for types `location` and `notification`.
127-
- `location`: the second parameter is a string, either `always` or `whenInUse`(default).
128-
- `notification`: the second parameter is an array with the desired alert types. Any combination of `alert`, `badge` and `sound` (default requests all three)
126+
Permission type `location` accepts a second parameter for `requestPermission` and `getPermissionStatus`; the second parameter is a string, either `always` or `whenInUse`(default).
127+
128+
Permission type `notification` accepts a second parameter for `requestPermission`. The second parameter is an array with the desired alert types. Any combination of `alert`, `badge` and `sound` (default requests all three)
129+
129130
```js
130131
///example
132+
Permissions.getPermissionStatus('location', 'always')
133+
.then(response => {
134+
this.setState({ locationPermission: response })
135+
})
136+
131137
Permissions.requestPermission('location', 'always')
132138
.then(response => {
133139
this.setState({ locationPermission: response })

0 commit comments

Comments
 (0)