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 } from "react-native-permissions" ;
119
1210// BEGIN EXTRA CODE
1311// END EXTRA CODE
@@ -20,7 +18,6 @@ export async function RequestLocationPermission(): Promise<boolean> {
2018 // BEGIN USER CODE
2119
2220 let reactNativeModule : typeof import ( "react-native" ) | undefined ;
23- let geolocationModule : typeof import ( "@react-native-community/geolocation" ) . default ;
2421
2522 const hasPermissionIOS = async ( ) : Promise < boolean > => {
2623 const openSetting = ( ) : void => {
@@ -29,68 +26,86 @@ export async function RequestLocationPermission(): Promise<boolean> {
2926 } ) ;
3027 } ;
3128
32- return ( geolocationModule as GeolocationServiceStatic )
33- . requestAuthorization ( "whenInUse" )
34- . then ( ( status : AuthorizationResult ) => {
35- if ( status === "granted" ) {
29+ try {
30+ const permission = PERMISSIONS . IOS . LOCATION_WHEN_IN_USE ;
31+ const result = await check ( permission ) ;
32+
33+ if ( result === RESULTS . GRANTED || result === RESULTS . LIMITED ) {
34+ return true ;
35+ }
36+
37+ if ( result === RESULTS . DENIED ) {
38+ const requestResult = await request ( permission ) ;
39+ if ( requestResult === RESULTS . GRANTED || requestResult === RESULTS . LIMITED ) {
3640 return true ;
3741 }
3842
39- if ( status === "denied" ) {
43+ if ( requestResult === RESULTS . DENIED ) {
4044 reactNativeModule ?. Alert . alert ( "Location permission denied." ) ;
4145 }
4246
43- if ( status === "disabled" ) {
47+ if ( requestResult === RESULTS . BLOCKED ) {
4448 reactNativeModule ?. Alert . alert (
4549 "Location Services must be enabled to determine your location." ,
4650 "" ,
47- [
48- { text : "Go to Settings" , onPress : openSetting } ,
49- {
50- text : "Don't Use Location"
51- }
52- ]
51+ [ { text : "Go to Settings" , onPress : openSetting } , { text : "Don't Use Location" } ]
5352 ) ;
5453 }
5554
5655 return false ;
57- } ) ;
56+ }
57+
58+ if ( result === RESULTS . BLOCKED ) {
59+ reactNativeModule ?. Alert . alert ( "Location Services must be enabled to determine your location." , "" , [
60+ { text : "Go to Settings" , onPress : openSetting } ,
61+ { text : "Don't Use Location" }
62+ ] ) ;
63+ return false ;
64+ }
65+ return false ;
66+ } catch {
67+ return false ;
68+ }
5869 } ;
5970
6071 const hasPermissionAndroid = async ( ) : Promise < boolean | undefined > => {
6172 if ( typeof reactNativeModule ?. Platform ?. Version === "number" && reactNativeModule ?. Platform ?. Version < 23 ) {
6273 return true ;
6374 }
6475
65- const androidLocationPermission = reactNativeModule ?. PermissionsAndroid . PERMISSIONS . ACCESS_FINE_LOCATION ;
76+ try {
77+ const permission = PERMISSIONS . ANDROID . ACCESS_FINE_LOCATION ;
78+ const result = await check ( permission ) ;
79+
80+ if ( result === RESULTS . GRANTED ) {
81+ return true ;
82+ }
83+
84+ if ( result === RESULTS . DENIED ) {
85+ const requestResult = await request ( permission ) ;
86+ if ( requestResult === RESULTS . GRANTED ) {
87+ return true ;
88+ }
89+
90+ if ( requestResult === RESULTS . DENIED ) {
91+ reactNativeModule ?. ToastAndroid ?. show (
92+ "Location permission denied by user." ,
93+ reactNativeModule . ToastAndroid . LONG
94+ ) ;
95+ } else if ( requestResult === RESULTS . BLOCKED ) {
96+ reactNativeModule ?. ToastAndroid ?. show (
97+ "Location permission revoked by user." ,
98+ reactNativeModule . ToastAndroid . LONG
99+ ) ;
100+ }
66101
67- if ( ! androidLocationPermission ) {
102+ return false ;
103+ }
104+
105+ return false ;
106+ } catch {
68107 return false ;
69108 }
70-
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- ) ;
94109 } ;
95110
96111 const hasLocationPermission = async ( ) : Promise < boolean > => {
@@ -107,49 +122,14 @@ export async function RequestLocationPermission(): Promise<boolean> {
107122 return Promise . reject ( new Error ( "Unsupported platform" ) ) ;
108123 } ;
109124
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-
137125 if ( navigator && navigator . product === "ReactNative" ) {
138126 reactNativeModule = require ( "react-native" ) ;
139127
140128 if ( ! reactNativeModule ) {
141129 return Promise . reject ( new Error ( "React Native module could not be found" ) ) ;
142130 }
143131
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- }
132+ return hasLocationPermission ( ) ;
153133 } else if ( navigator && navigator . geolocation ) {
154134 return Promise . reject ( new Error ( "No permission request for location is required for web/hybrid platform" ) ) ;
155135 } else {
0 commit comments