Example.js 4.6KB

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