123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.getTypes()
  24. let canOpenSettings = Permissions.canOpenSettings()
  25. this.setState({ types, canOpenSettings })
  26. this._updatePermissions(types)
  27. AppState.addEventListener('change', this._handleAppStateChange.bind(this));
  28. }
  29. componentWillUnmount() {
  30. AppState.removeEventListener('change', this._handleAppStateChange.bind(this));
  31. }
  32. //update permissions when app comes back from settings
  33. _handleAppStateChange(appState) {
  34. if (appState == 'active') {
  35. this._updatePermissions(this.state.types)
  36. }
  37. }
  38. _openSettings() {
  39. return Permissions.openSettings()
  40. .then(() => alert('back to app!!'))
  41. }
  42. _updatePermissions(types) {
  43. Permissions.checkMultiple(types)
  44. .then(status => {
  45. if (this.state.isAlways) {
  46. return Permissions.check('location', 'always')
  47. .then(location => ({...status, location}))
  48. }
  49. return status
  50. })
  51. .then(status => this.setState({ status }))
  52. }
  53. _requestPermission(permission) {
  54. var options
  55. if (permission == 'location') {
  56. options = this.state.isAlways ? 'always' : 'whenInUse'
  57. }
  58. Permissions.request(permission, options)
  59. .then(res => {
  60. this.setState({
  61. status: {...this.state.status, [permission]: res}
  62. })
  63. if (res != 'authorized') {
  64. var buttons = [{ text: 'Cancel', style: 'cancel' }]
  65. if (this.state.canOpenSettings) buttons.push({ text: 'Open Settings', onPress: this._openSettings.bind(this) })
  66. Alert.alert(
  67. 'Whoops!',
  68. "There was a problem getting your permission. Please enable it from settings.",
  69. buttons
  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. {
  104. this.state.canOpenSettings &&
  105. <TouchableHighlight
  106. onPress={this._openSettings.bind(this)}>
  107. <Text style={styles.text}>Open settings</Text>
  108. </TouchableHighlight>
  109. }
  110. </View>
  111. <Text style={styles['footer_'+Platform.OS]}>
  112. 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"
  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. })