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