55// - the code between BEGIN USER CODE and END USER CODE
66// - the code between BEGIN EXTRA CODE and END EXTRA CODE
77// Other code you write will be lost the next time you deploy the project.
8- import Geolocation from "@react-native-community/geolocation" ;
9-
10- import type { GeolocationServiceStatic , AuthorizationResult } from "../../typings/Geolocation" ;
8+ import { check , request , PERMISSIONS , RESULTS , openSettings } from "react-native-permissions" ;
9+ import { Platform , Alert , ToastAndroid } from "react-native" ;
1110
1211// BEGIN EXTRA CODE
1312// END EXTRA CODE
@@ -19,137 +18,77 @@ import type { GeolocationServiceStatic, AuthorizationResult } from "../../typing
1918export async function RequestLocationPermission ( ) : Promise < boolean > {
2019 // BEGIN USER CODE
2120
22- let reactNativeModule : typeof import ( "react-native" ) | undefined ;
23- let geolocationModule : typeof import ( "@react-native-community/geolocation" ) . default ;
24-
2521 const hasPermissionIOS = async ( ) : Promise < boolean > => {
2622 const openSetting = ( ) : void => {
27- reactNativeModule ?. Linking . openSettings ( ) . catch ( ( ) => {
28- reactNativeModule ?. Alert . alert ( "Unable to open settings." ) ;
23+ openSettings ( ) . catch ( ( ) => {
24+ Alert . alert ( "Unable to open settings." ) ;
2925 } ) ;
3026 } ;
3127
32- return ( geolocationModule as GeolocationServiceStatic )
33- . requestAuthorization ( "whenInUse" )
34- . then ( ( status : AuthorizationResult ) => {
35- if ( status === "granted" ) {
36- return true ;
37- }
28+ const status = await request ( PERMISSIONS . IOS . LOCATION_WHEN_IN_USE ) ;
3829
39- if ( status === "denied" ) {
40- reactNativeModule ?. Alert . alert ( "Location permission denied." ) ;
41- }
30+ if ( status === RESULTS . GRANTED ) {
31+ return true ;
32+ }
33+
34+ if ( status === RESULTS . DENIED || status === RESULTS . BLOCKED ) {
35+ Alert . alert ( "Location permission denied." ) ;
36+ }
4237
43- if ( status === "disabled" ) {
44- reactNativeModule ?. Alert . alert (
45- "Location Services must be enabled to determine your location." ,
46- "" ,
47- [
48- { text : "Go to Settings" , onPress : openSetting } ,
49- {
50- text : "Don't Use Location"
51- }
52- ]
53- ) ;
38+ if ( status === RESULTS . UNAVAILABLE ) {
39+ Alert . alert ( "Location Services must be enabled to determine your location." , "" , [
40+ { text : "Go to Settings" , onPress : openSetting } ,
41+ {
42+ text : "Don't Use Location"
5443 }
44+ ] ) ;
45+ }
5546
56- return false ;
57- } ) ;
47+ return false ;
5848 } ;
5949
6050 const hasPermissionAndroid = async ( ) : Promise < boolean | undefined > => {
61- if ( typeof reactNativeModule ?. Platform ? .Version === "number" && reactNativeModule ?. Platform ? .Version < 23 ) {
51+ if ( typeof Platform . Version === "number" && Platform . Version < 23 ) {
6252 return true ;
6353 }
6454
65- const androidLocationPermission = reactNativeModule ?. PermissionsAndroid . PERMISSIONS . ACCESS_FINE_LOCATION ;
55+ const status = await check ( PERMISSIONS . ANDROID . ACCESS_FINE_LOCATION ) ;
6656
67- if ( ! androidLocationPermission ) {
68- return false ;
57+ if ( status === RESULTS . GRANTED ) {
58+ return true ;
6959 }
7060
71- return reactNativeModule ?. PermissionsAndroid . check ( androidLocationPermission ) . then ( hasPermission =>
72- hasPermission
73- ? true
74- : reactNativeModule ?. PermissionsAndroid ?. request ( androidLocationPermission ) . then ( status => {
75- if ( status === reactNativeModule ?. PermissionsAndroid . RESULTS . GRANTED ) {
76- return true ;
77- }
78-
79- if ( status === reactNativeModule ?. PermissionsAndroid . RESULTS . DENIED ) {
80- reactNativeModule . ToastAndroid . show (
81- "Location permission denied by user." ,
82- reactNativeModule . ToastAndroid . LONG
83- ) ;
84- } else if ( status === reactNativeModule ?. PermissionsAndroid . RESULTS . NEVER_ASK_AGAIN ) {
85- reactNativeModule . ToastAndroid . show (
86- "Location permission revoked by user." ,
87- reactNativeModule . ToastAndroid . LONG
88- ) ;
89- }
90-
91- return false ;
92- } )
93- ) ;
61+ const requestStatus = await request ( PERMISSIONS . ANDROID . ACCESS_FINE_LOCATION ) ;
62+
63+ if ( requestStatus === RESULTS . GRANTED ) {
64+ return true ;
65+ }
66+
67+ if ( requestStatus === RESULTS . DENIED ) {
68+ ToastAndroid . show ( "Location permission denied by user." , ToastAndroid . LONG ) ;
69+ } else if ( requestStatus === RESULTS . BLOCKED ) {
70+ ToastAndroid . show ( "Location permission revoked by user." , ToastAndroid . LONG ) ;
71+ }
72+
73+ return false ;
9474 } ;
9575
9676 const hasLocationPermission = async ( ) : Promise < boolean > => {
97- if ( reactNativeModule ?. Platform . OS === "ios" ) {
77+ if ( Platform . OS === "ios" ) {
9878 const hasPermission = await hasPermissionIOS ( ) ;
9979 return hasPermission ;
10080 }
10181
102- if ( reactNativeModule ?. Platform . OS === "android" ) {
82+ if ( Platform . OS === "android" ) {
10383 const hasPermission = await hasPermissionAndroid ( ) ;
10484 return hasPermission ?? false ;
10585 }
10686
10787 return Promise . reject ( new Error ( "Unsupported platform" ) ) ;
10888 } ;
10989
110- const hasLocationPermissionForOldLibrary = async ( ) : Promise < boolean | undefined > => {
111- if ( reactNativeModule ?. Platform . OS === "android" ) {
112- const locationPermission = reactNativeModule . PermissionsAndroid . PERMISSIONS . ACCESS_FINE_LOCATION ;
113-
114- return reactNativeModule . PermissionsAndroid . check ( locationPermission ) . then ( hasPermission =>
115- hasPermission
116- ? true
117- : reactNativeModule ?. PermissionsAndroid . request ( locationPermission ) . then (
118- status => status === reactNativeModule ?. PermissionsAndroid . RESULTS . GRANTED
119- )
120- ) ;
121- } else if ( geolocationModule ) {
122- return new Promise ( resolve => {
123- geolocationModule . requestAuthorization (
124- ( ) => {
125- resolve ( true ) ;
126- } ,
127- ( ) => {
128- resolve ( false ) ;
129- }
130- ) ;
131- } ) ;
132- }
133-
134- return false ;
135- } ;
136-
13790 if ( navigator && navigator . product === "ReactNative" ) {
138- reactNativeModule = require ( "react-native" ) ;
139-
140- if ( ! reactNativeModule ) {
141- return Promise . reject ( new Error ( "React Native module could not be found" ) ) ;
142- }
143-
144- if ( reactNativeModule . NativeModules . RNFusedLocation ) {
145- geolocationModule = ( await import ( "@react-native-community/geolocation" ) ) . default ;
146- return hasLocationPermission ( ) ;
147- } else if ( reactNativeModule . NativeModules . RNCGeolocation ) {
148- geolocationModule = Geolocation ;
149- return ( await hasLocationPermissionForOldLibrary ( ) ) ?? false ;
150- } else {
151- return Promise . reject ( new Error ( "Geolocation module could not be found" ) ) ;
152- }
91+ return hasLocationPermission ( ) ;
15392 } else if ( navigator && navigator . geolocation ) {
15493 return Promise . reject ( new Error ( "No permission request for location is required for web/hybrid platform" ) ) ;
15594 } else {
0 commit comments