ソースを参照

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

Yonah Forst 8 年 前
コミット
d0d0c18e80
3 ファイル変更57 行追加14 行削除
  1. 45 10
      Example/Example.js
  2. 2 0
      Example/ios/Example/Info.plist
  3. 10 4
      README.md

+ 45 - 10
Example/Example.js

@@ -12,6 +12,7 @@ import {
   View,
   Alert,
   AppState,
+  Platform,
 } from 'react-native';
 
 import Permissions from 'react-native-permissions'
@@ -42,11 +43,24 @@ export default class Example extends Component {
 
   _updatePermissions(types) {
     Permissions.checkMultiplePermissions(types)
+      .then(status => {
+        if (this.state.isAlways) {
+          return Permissions.getPermissionStatus('location', 'always')
+            .then(location => ({...status, location}))
+        }
+        return status
+      })
       .then(status => this.setState({ status }))
   }
 
   _requestPermission(permission) {
-    Permissions.requestPermission(permission)
+    var options
+
+    if (permission == 'location') {
+      options = this.state.isAlways ? 'always' : 'whenInUse'
+    }
+
+    Permissions.requestPermission(permission, options)
       .then(res => {
         this.setState({
           status: {...this.state.status, [permission]: res}
@@ -64,9 +78,15 @@ export default class Example extends Component {
       }).catch(e => console.warn(e))
   }
 
+  _onLocationSwitchChange() {
+    this.setState({ isAlways: !this.state.isAlways })
+    this._updatePermissions(this.state.types)
+  }
+
   render() {
     return (
       <View style={styles.container}>
+
         {this.state.types.map(p => (
           <TouchableHighlight 
             style={[styles.button, styles[this.state.status[p]]]}
@@ -74,7 +94,7 @@ export default class Example extends Component {
             onPress={this._requestPermission.bind(this, p)}>
             <View>
               <Text style={styles.text}>
-                {p}
+                {Platform.OS == 'ios' && p == 'location' ? `location ${this.state.isAlways ? 'always' : 'whenInUse'}` : p}
               </Text>
               <Text style={styles.subtext}>
                 {this.state.status[p]}
@@ -83,13 +103,23 @@ export default class Example extends Component {
           </TouchableHighlight>
           )
         )}
-        <TouchableHighlight 
-          style={styles.openSettings}
-          onPress={Permissions.openSettings}>
-          <Text style={styles.text}>Open settings</Text>
-        </TouchableHighlight>
+        <View style={styles.footer}>
+          <TouchableHighlight 
+            style={styles['footer_'+Platform.OS]}
+            onPress={this._onLocationSwitchChange.bind(this)}>
+            <Text style={styles.text}>Toggle location type</Text>
+          </TouchableHighlight>
 
-        <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>
+          <TouchableHighlight 
+            onPress={Permissions.openSettings}>
+            <Text style={styles.text}>Open settings</Text>
+          </TouchableHighlight>
+        </View>
+
+
+        <Text style={styles['footer_'+Platform.OS]}>
+          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>
       </View>
     );
   }
@@ -130,8 +160,13 @@ const styles = StyleSheet.create({
   restricted: {
     backgroundColor: '#FFAB91'
   },
-  openSettings: {
+  footer: {
     padding: 10,
-    alignSelf: 'flex-end',
+    flexDirection: 'row',
+    justifyContent: 'space-between',
+  },
+  footer_android: {
+    height: 0,
+    width: 0,
   }
 })

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

@@ -41,6 +41,8 @@
 	<string>test</string>
 	<key>NSContactsUsageDescription</key>
 	<string>test</string>
+	<key>NSLocationAlwaysUsageDescription</key>
+	<string>test</string>
 	<key>NSLocationWhenInUseUsageDescription</key>
 	<string>test</string>
 	<key>NSMicrophoneUsageDescription</key>

+ 10 - 4
README.md

@@ -113,7 +113,7 @@ Promises resolve into one of these statuses
 ###Methods
 | Method Name | Arguments | Notes
 |---|---|---|
-| `getPermissionStatus` | `type` | - Returns a promise with the permission status. Note: for type `location`, iOS `AuthorizedAlways` and `AuthorizedWhenInUse` both return `authorized` |
+| `getPermissionStatus` | `type` | - Returns a promise with the permission status. See iOS Notes for special cases |
 | `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|
 | `checkMultiplePermissions` | `[types]` | - Accepts an array of permission types and returns a promise with an object mapping permission types to statuses |
 | `getPermissionTypes` | *none* | - Returns an array of valid permission types  |
@@ -123,11 +123,17 @@ Promises resolve into one of these statuses
 ###iOS Notes
 Permission type `bluetooth` represents the status of the `CBPeripheralManager`. Don't use this if only need `CBCentralManager`
 
-`requestPermission` also accepts a second parameter for types `location` and `notification`.
-- `location`: the second parameter is a string, either `always` or `whenInUse`(default).
-- `notification`: the second parameter is an array with the desired alert types. Any combination of `alert`, `badge` and `sound` (default requests all three)
+Permission type `location` accepts a second parameter for `requestPermission` and `getPermissionStatus`;  the second parameter is a string, either `always` or `whenInUse`(default).
+
+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)
+
 ```js
 ///example
+    Permissions.getPermissionStatus('location', 'always')
+      .then(response => {
+        this.setState({ locationPermission: response })
+      })
+
     Permissions.requestPermission('location', 'always')
       .then(response => {
         this.setState({ locationPermission: response })