Skip to content

Commit 3b4d22f

Browse files
Merge pull request #46 from douglasjunior/add-android-permission-check-before-request
Add android permission check before request #45
2 parents 87a44af + 109fe90 commit 3b4d22f

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-native-get-location",
33
"displayName": "React-Native Get Location",
4-
"version": "4.0.0",
4+
"version": "4.0.1",
55
"description": "⚛ Simple to use React Native library to get device location for Android and iOS.",
66
"main": "src",
77
"license": "MIT",

src/utils.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
* SOFTWARE.
2323
*/
2424

25-
import {PermissionsAndroid, Rationale} from 'react-native';
25+
import { PermissionsAndroid, Rationale } from 'react-native';
2626

2727
import LocationError from './LocationError';
2828

29+
const { PERMISSIONS, RESULTS } = PermissionsAndroid;
30+
2931
export type Location = {
3032
/**
3133
* The latitude, in degrees
@@ -93,14 +95,15 @@ export async function requestAndroidPermission(
9395
enableHighAccuracy: boolean = false,
9496
rationale?: Rationale,
9597
) {
96-
const {PERMISSIONS, RESULTS} = PermissionsAndroid;
98+
const permission = enableHighAccuracy
99+
? PERMISSIONS.ACCESS_FINE_LOCATION
100+
: PERMISSIONS.ACCESS_COARSE_LOCATION
101+
102+
const alreadyGranted = await PermissionsAndroid.check(permission);
103+
104+
if (alreadyGranted) return true;
97105

98-
const granted = await PermissionsAndroid.request(
99-
enableHighAccuracy
100-
? PERMISSIONS.ACCESS_FINE_LOCATION
101-
: PERMISSIONS.ACCESS_COARSE_LOCATION,
102-
rationale,
103-
);
106+
const granted = await PermissionsAndroid.request(permission, rationale);
104107

105108
if (granted !== RESULTS.GRANTED) {
106109
throw new LocationError('UNAUTHORIZED', 'Authorization denied');

0 commit comments

Comments
 (0)