Quellcode durchsuchen

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

Yonah Forst vor 7 Jahren
Ursprung
Commit
d0d0c18e80
3 geänderte Dateien mit 57 neuen und 14 gelöschten Zeilen
  1. 45
    10
      Example/Example.js
  2. 2
    0
      Example/ios/Example/Info.plist
  3. 10
    4
      README.md

+ 45
- 10
Example/Example.js Datei anzeigen

@@ -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
 })

+ 2
- 0
Example/ios/Example/Info.plist Datei anzeigen

@@ -41,6 +41,8 @@
41 41
 	<string>test</string>
42 42
 	<key>NSContactsUsageDescription</key>
43 43
 	<string>test</string>
44
+	<key>NSLocationAlwaysUsageDescription</key>
45
+	<string>test</string>
44 46
 	<key>NSLocationWhenInUseUsageDescription</key>
45 47
 	<string>test</string>
46 48
 	<key>NSMicrophoneUsageDescription</key>

+ 10
- 4
README.md Datei anzeigen

@@ -113,7 +113,7 @@ Promises resolve into one of these statuses
113 113
 ###Methods
114 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 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 118
 | `checkMultiplePermissions` | `[types]` | - Accepts an array of permission types and returns a promise with an object mapping permission types to statuses |
119 119
 | `getPermissionTypes` | *none* | - Returns an array of valid permission types  |
@@ -123,11 +123,17 @@ Promises resolve into one of these statuses
123 123
 ###iOS Notes
124 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 130
 ```js
130 131
 ///example
132
+    Permissions.getPermissionStatus('location', 'always')
133
+      .then(response => {
134
+        this.setState({ locationPermission: response })
135
+      })
136
+
131 137
     Permissions.requestPermission('location', 'always')
132 138
       .then(response => {
133 139
         this.setState({ locationPermission: response })