forked from Darren80/fog-o-war
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocationPermissions.js
55 lines (47 loc) · 1.97 KB
/
locationPermissions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React from 'react';
import { Button, View, StyleSheet } from 'react-native';
import * as TaskManager from 'expo-task-manager';
import * as Location from 'expo-location';
const LOCATION_TASK_NAME = 'background-location-task';
export function requestPermissions() {
TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
if (error) {
// Error occurred - check `error.message` for more details.
return;
}
if (data) {
const { locations } = data;
//TODO: Track location even when app is in the background.
// do something with the locations captured in the background
}
});
return Location.requestForegroundPermissionsAsync()
.then(({ status: foregroundStatus }) => {
if (foregroundStatus === 'granted') {
return Location.requestBackgroundPermissionsAsync();
} else {
console.log("Permission to access location was denied");
throw new Error("Permission to access location was denied");
}
})
.then(({ status: backgroundStatus }) => {
if (backgroundStatus === 'granted') {
//Will run expo-taskmanager if location changes while app is in the background
Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
accuracy: Location.Accuracy.Highest,
deferredUpdatesDistance: 20,
deferredUpdatesInterval: 1000
});
} else {
console.log("Permission to access location in the background was denied");
throw new Error("Permission to access location in the background was denied");
}
})
const PermissionsButton = () => (
<View style={styles.container}>
<Button onPress={requestPermissions} title="Enable background location" />
</View>
);
}
const styles = StyleSheet.create({
});