Browse Source

update example and readme for pr #74 (Allow location type to be specified in getPermissionStatus)

Yonah Forst 7 years ago
parent
commit
d0d0c18e80
3 changed files with 57 additions and 14 deletions
  1. 45
    10
      Example/Example.js
  2. 2
    0
      Example/ios/Example/Info.plist
  3. 10
    4
      README.md

+ 45
- 10
Example/Example.js View File

12
   View,
12
   View,
13
   Alert,
13
   Alert,
14
   AppState,
14
   AppState,
15
+  Platform,
15
 } from 'react-native';
16
 } from 'react-native';
16
 
17
 
17
 import Permissions from 'react-native-permissions'
18
 import Permissions from 'react-native-permissions'
42
 
43
 
43
   _updatePermissions(types) {
44
   _updatePermissions(types) {
44
     Permissions.checkMultiplePermissions(types)
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
       .then(status => this.setState({ status }))
53
       .then(status => this.setState({ status }))
46
   }
54
   }
47
 
55
 
48
   _requestPermission(permission) {
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
       .then(res => {
64
       .then(res => {
51
         this.setState({
65
         this.setState({
52
           status: {...this.state.status, [permission]: res}
66
           status: {...this.state.status, [permission]: res}
64
       }).catch(e => console.warn(e))
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
   render() {
86
   render() {
68
     return (
87
     return (
69
       <View style={styles.container}>
88
       <View style={styles.container}>
89
+
70
         {this.state.types.map(p => (
90
         {this.state.types.map(p => (
71
           <TouchableHighlight 
91
           <TouchableHighlight 
72
             style={[styles.button, styles[this.state.status[p]]]}
92
             style={[styles.button, styles[this.state.status[p]]]}
74
             onPress={this._requestPermission.bind(this, p)}>
94
             onPress={this._requestPermission.bind(this, p)}>
75
             <View>
95
             <View>
76
               <Text style={styles.text}>
96
               <Text style={styles.text}>
77
-                {p}
97
+                {Platform.OS == 'ios' && p == 'location' ? `location ${this.state.isAlways ? 'always' : 'whenInUse'}` : p}
78
               </Text>
98
               </Text>
79
               <Text style={styles.subtext}>
99
               <Text style={styles.subtext}>
80
                 {this.state.status[p]}
100
                 {this.state.status[p]}
83
           </TouchableHighlight>
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
       </View>
123
       </View>
94
     );
124
     );
95
   }
125
   }
130
   restricted: {
160
   restricted: {
131
     backgroundColor: '#FFAB91'
161
     backgroundColor: '#FFAB91'
132
   },
162
   },
133
-  openSettings: {
163
+  footer: {
134
     padding: 10,
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
 })

+ 2
- 0
Example/ios/Example/Info.plist View File

41
 	<string>test</string>
41
 	<string>test</string>
42
 	<key>NSContactsUsageDescription</key>
42
 	<key>NSContactsUsageDescription</key>
43
 	<string>test</string>
43
 	<string>test</string>
44
+	<key>NSLocationAlwaysUsageDescription</key>
45
+	<string>test</string>
44
 	<key>NSLocationWhenInUseUsageDescription</key>
46
 	<key>NSLocationWhenInUseUsageDescription</key>
45
 	<string>test</string>
47
 	<string>test</string>
46
 	<key>NSMicrophoneUsageDescription</key>
48
 	<key>NSMicrophoneUsageDescription</key>

+ 10
- 4
README.md View File

113
 ###Methods
113
 ###Methods
114
 | Method Name | Arguments | Notes
114
 | Method Name | Arguments | Notes
115
 |---|---|---|
115
 |---|---|---|
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 |
117
 | `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|
117
 | `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|
118
 | `checkMultiplePermissions` | `[types]` | - Accepts an array of permission types and returns a promise with an object mapping permission types to statuses |
118
 | `checkMultiplePermissions` | `[types]` | - Accepts an array of permission types and returns a promise with an object mapping permission types to statuses |
119
 | `getPermissionTypes` | *none* | - Returns an array of valid permission types  |
119
 | `getPermissionTypes` | *none* | - Returns an array of valid permission types  |
123
 ###iOS Notes
123
 ###iOS Notes
124
 Permission type `bluetooth` represents the status of the `CBPeripheralManager`. Don't use this if only need `CBCentralManager`
124
 Permission type `bluetooth` represents the status of the `CBPeripheralManager`. Don't use this if only need `CBCentralManager`
125
 
125
 
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
+
129
 ```js
130
 ```js
130
 ///example
131
 ///example
132
+    Permissions.getPermissionStatus('location', 'always')
133
+      .then(response => {
134
+        this.setState({ locationPermission: response })
135
+      })
136
+
131
     Permissions.requestPermission('location', 'always')
137
     Permissions.requestPermission('location', 'always')
132
       .then(response => {
138
       .then(response => {
133
         this.setState({ locationPermission: response })
139
         this.setState({ locationPermission: response })