App.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // @flow
  2. import React, {Component} from 'react';
  3. import {
  4. StyleSheet,
  5. TouchableHighlight,
  6. Text,
  7. View,
  8. Alert,
  9. AppState,
  10. Platform,
  11. } from 'react-native';
  12. import Permissions from 'react-native-permissions';
  13. export default class App extends Component {
  14. state = {
  15. types: [],
  16. status: {},
  17. };
  18. componentDidMount() {
  19. let types = Permissions.getTypes();
  20. let canOpenSettings = Permissions.canOpenSettings();
  21. this.setState({types, canOpenSettings});
  22. this._updatePermissions(types);
  23. AppState.addEventListener('change', this._handleAppStateChange);
  24. }
  25. componentWillUnmount() {
  26. AppState.removeEventListener('change', this._handleAppStateChange);
  27. }
  28. //update permissions when app comes back from settings
  29. _handleAppStateChange = appState => {
  30. if (appState == 'active') {
  31. this._updatePermissions(this.state.types);
  32. }
  33. };
  34. _openSettings = () =>
  35. Permissions.openSettings().then(() => alert('back to app!!'));
  36. _updatePermissions = types => {
  37. Permissions.checkMultiple(types)
  38. .then(status => {
  39. if (this.state.isAlways) {
  40. return Permissions.check('location', 'always').then(location => ({
  41. ...status,
  42. location,
  43. }));
  44. }
  45. return status;
  46. })
  47. .then(status => this.setState({status}));
  48. };
  49. _requestPermission = permission => {
  50. var options;
  51. if (permission == 'location') {
  52. options = this.state.isAlways ? 'always' : 'whenInUse';
  53. }
  54. Permissions.request(permission, options)
  55. .then(res => {
  56. this.setState({
  57. status: {...this.state.status, [permission]: res},
  58. });
  59. if (res != 'authorized') {
  60. var buttons = [{text: 'Cancel', style: 'cancel'}];
  61. if (this.state.canOpenSettings)
  62. buttons.push({
  63. text: 'Open Settings',
  64. onPress: this._openSettings,
  65. });
  66. Alert.alert(
  67. 'Whoops!',
  68. 'There was a problem getting your permission. Please enable it from settings.',
  69. buttons,
  70. );
  71. }
  72. })
  73. .catch(e => console.warn(e));
  74. };
  75. _onLocationSwitchChange = () => {
  76. this.setState({isAlways: !this.state.isAlways});
  77. this._updatePermissions(this.state.types);
  78. };
  79. render() {
  80. return (
  81. <View style={styles.container}>
  82. {this.state.types.map(p => (
  83. <TouchableHighlight
  84. style={[styles.button, styles[this.state.status[p]]]}
  85. key={p}
  86. onPress={() => this._requestPermission(p)}>
  87. <View>
  88. <Text style={styles.text}>
  89. {Platform.OS == 'ios' && p == 'location'
  90. ? `location ${this.state.isAlways ? 'always' : 'whenInUse'}`
  91. : p}
  92. </Text>
  93. <Text style={styles.subtext}>{this.state.status[p]}</Text>
  94. </View>
  95. </TouchableHighlight>
  96. ))}
  97. <View style={styles.footer}>
  98. <TouchableHighlight
  99. style={styles['footer_' + Platform.OS]}
  100. onPress={this._onLocationSwitchChange}>
  101. <Text style={styles.text}>Toggle location type</Text>
  102. </TouchableHighlight>
  103. {this.state.canOpenSettings && (
  104. <TouchableHighlight onPress={this._openSettings}>
  105. <Text style={styles.text}>Open settings</Text>
  106. </TouchableHighlight>
  107. )}
  108. </View>
  109. <Text style={styles['footer_' + Platform.OS]}>
  110. Note: microphone permissions may not work on iOS simulator. Also,
  111. toggling permissions from the settings menu may cause the app to
  112. crash. This is normal on iOS. Google "ios crash permission change"
  113. </Text>
  114. </View>
  115. );
  116. }
  117. }
  118. const styles = StyleSheet.create({
  119. container: {
  120. flex: 1,
  121. justifyContent: 'center',
  122. backgroundColor: '#F5FCFF',
  123. padding: 10,
  124. },
  125. text: {
  126. textAlign: 'center',
  127. fontWeight: 'bold',
  128. },
  129. subtext: {
  130. textAlign: 'center',
  131. },
  132. button: {
  133. margin: 5,
  134. borderColor: 'black',
  135. borderWidth: 3,
  136. overflow: 'hidden',
  137. },
  138. buttonInner: {
  139. flexDirection: 'column',
  140. },
  141. undetermined: {
  142. backgroundColor: '#E0E0E0',
  143. },
  144. authorized: {
  145. backgroundColor: '#C5E1A5',
  146. },
  147. denied: {
  148. backgroundColor: '#ef9a9a',
  149. },
  150. restricted: {
  151. backgroundColor: '#ef9a9a',
  152. },
  153. footer: {
  154. padding: 10,
  155. flexDirection: 'row',
  156. justifyContent: 'space-between',
  157. },
  158. footer_android: {
  159. height: 0,
  160. width: 0,
  161. },
  162. });