123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. } from 'react-native';
  15. import Permissions from 'react-native-permissions'
  16. export default class Example extends Component {
  17. state = {
  18. types: [],
  19. status: {},
  20. }
  21. componentDidMount() {
  22. let types = Permissions.getPermissionTypes()
  23. this.setState({ types })
  24. this._updatePermissions(types)
  25. AppState.addEventListener('change', this._handleAppStateChange.bind(this));
  26. }
  27. componentWillUnmount() {
  28. AppState.removeEventListener('change', this._handleAppStateChange.bind(this));
  29. }
  30. //update permissions when app comes back from settings
  31. _handleAppStateChange(appState) {
  32. if (appState == 'active') {
  33. this._updatePermissions(this.state.types)
  34. }
  35. }
  36. _updatePermissions(types) {
  37. Permissions.checkMultiplePermissions(types)
  38. .then(status => this.setState({ status }))
  39. }
  40. _requestPermission(permission) {
  41. Permissions.requestPermission(permission)
  42. .then(res => {
  43. this.setState({
  44. status: {...this.state.status, [permission]: res}
  45. })
  46. if (res != 'authorized') {
  47. Alert.alert(
  48. 'Whoops!',
  49. "There was a problem getting your permission. Please enable it from settings.",
  50. [
  51. {text: 'Cancel', style: 'cancel'},
  52. {text: 'Open Settings', onPress: Permissions.openSettings },
  53. ]
  54. )
  55. }
  56. }).catch(e => console.warn(e))
  57. }
  58. render() {
  59. return (
  60. <View style={styles.container}>
  61. {this.state.types.map(p => (
  62. <TouchableHighlight
  63. style={[styles.button, styles[this.state.status[p]]]}
  64. key={p}
  65. onPress={this._requestPermission.bind(this, p)}>
  66. <View>
  67. <Text style={styles.text}>
  68. {p}
  69. </Text>
  70. <Text style={styles.subtext}>
  71. {this.state.status[p]}
  72. </Text>
  73. </View>
  74. </TouchableHighlight>
  75. )
  76. )}
  77. <TouchableHighlight
  78. style={styles.openSettings}
  79. onPress={Permissions.openSettings}>
  80. <Text style={styles.text}>Open settings</Text>
  81. </TouchableHighlight>
  82. <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>
  83. </View>
  84. );
  85. }
  86. }
  87. const styles = StyleSheet.create({
  88. container: {
  89. flex: 1,
  90. justifyContent: 'center',
  91. backgroundColor: '#F5FCFF',
  92. padding: 10,
  93. },
  94. text: {
  95. textAlign: 'center',
  96. fontWeight: 'bold',
  97. },
  98. subtext: {
  99. textAlign: 'center',
  100. },
  101. button: {
  102. margin: 5,
  103. borderColor: 'black',
  104. borderWidth: 3,
  105. overflow: 'hidden',
  106. },
  107. buttonInner: {
  108. flexDirection: 'column',
  109. },
  110. undetermined: {
  111. backgroundColor: '#E0E0E0',
  112. },
  113. authorized: {
  114. backgroundColor: '#C5E1A5',
  115. },
  116. denied: {
  117. backgroundColor: '#ef9a9a',
  118. },
  119. restricted: {
  120. backgroundColor: '#FFAB91'
  121. },
  122. openSettings: {
  123. padding: 10,
  124. alignSelf: 'flex-end',
  125. }
  126. })