@@ -12,6 +12,7 @@ import {
1212 View ,
1313 Alert ,
1414 AppState ,
15+ Platform ,
1516} from 'react-native' ;
1617
1718import Permissions from 'react-native-permissions'
@@ -40,13 +41,31 @@ export default class Example extends Component {
4041 }
4142 }
4243
44+ _openSettings ( ) {
45+ return Permissions . openSettings ( )
46+ . then ( ( ) => alert ( 'back to app!!' ) )
47+ }
48+
4349 _updatePermissions ( types ) {
4450 Permissions . checkMultiplePermissions ( types )
51+ . then ( status => {
52+ if ( this . state . isAlways ) {
53+ return Permissions . getPermissionStatus ( 'location' , 'always' )
54+ . then ( location => ( { ...status , location} ) )
55+ }
56+ return status
57+ } )
4558 . then ( status => this . setState ( { status } ) )
4659 }
4760
4861 _requestPermission ( permission ) {
49- Permissions . requestPermission ( permission )
62+ var options
63+
64+ if ( permission == 'location' ) {
65+ options = this . state . isAlways ? 'always' : 'whenInUse'
66+ }
67+
68+ Permissions . requestPermission ( permission , options )
5069 . then ( res => {
5170 this . setState ( {
5271 status : { ...this . state . status , [ permission ] : res }
@@ -57,24 +76,30 @@ export default class Example extends Component {
5776 "There was a problem getting your permission. Please enable it from settings." ,
5877 [
5978 { text : 'Cancel' , style : 'cancel' } ,
60- { text : 'Open Settings' , onPress : Permissions . openSettings } ,
79+ { text : 'Open Settings' , onPress : this . _openSettings . bind ( this ) } ,
6180 ]
6281 )
6382 }
6483 } ) . catch ( e => console . warn ( e ) )
6584 }
6685
86+ _onLocationSwitchChange ( ) {
87+ this . setState ( { isAlways : ! this . state . isAlways } )
88+ this . _updatePermissions ( this . state . types )
89+ }
90+
6791 render ( ) {
6892 return (
6993 < View style = { styles . container } >
94+
7095 { this . state . types . map ( p => (
7196 < TouchableHighlight
7297 style = { [ styles . button , styles [ this . state . status [ p ] ] ] }
7398 key = { p }
7499 onPress = { this . _requestPermission . bind ( this , p ) } >
75100 < View >
76101 < Text style = { styles . text } >
77- { p }
102+ { Platform . OS == 'ios' && p == 'location' ? `location ${ this . state . isAlways ? 'always' : 'whenInUse' } ` : p }
78103 </ Text >
79104 < Text style = { styles . subtext } >
80105 { this . state . status [ p ] }
@@ -83,13 +108,23 @@ export default class Example extends Component {
83108 </ TouchableHighlight >
84109 )
85110 ) }
86- < TouchableHighlight
87- style = { styles . openSettings }
88- onPress = { Permissions . openSettings } >
89- < Text style = { styles . text } > Open settings</ Text >
90- </ TouchableHighlight >
111+ < View style = { styles . footer } >
112+ < TouchableHighlight
113+ style = { styles [ 'footer_' + Platform . OS ] }
114+ onPress = { this . _onLocationSwitchChange . bind ( this ) } >
115+ < Text style = { styles . text } > Toggle location type</ Text >
116+ </ TouchableHighlight >
117+
118+ < TouchableHighlight
119+ onPress = { this . _openSettings . bind ( this ) } >
120+ < Text style = { styles . text } > Open settings</ Text >
121+ </ TouchableHighlight >
122+ </ View >
91123
92- < 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 >
124+
125+ < Text style = { styles [ 'footer_' + Platform . OS ] } >
126+ 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"
127+ </ Text >
93128 </ View >
94129 ) ;
95130 }
@@ -130,8 +165,13 @@ const styles = StyleSheet.create({
130165 restricted : {
131166 backgroundColor : '#FFAB91'
132167 } ,
133- openSettings : {
168+ footer : {
134169 padding : 10 ,
135- alignSelf : 'flex-end' ,
170+ flexDirection : 'row' ,
171+ justifyContent : 'space-between' ,
172+ } ,
173+ footer_android : {
174+ height : 0 ,
175+ width : 0 ,
136176 }
137177} )
0 commit comments