Skip to content

Instantly share code, notes, and snippets.

@aloukissas
Created January 16, 2019 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aloukissas/161d9637f985cb0cfe6103cf5f5c8247 to your computer and use it in GitHub Desktop.
Save aloukissas/161d9637f985cb0cfe6103cf5f5c8247 to your computer and use it in GitHub Desktop.
import React from "react";
import { View, Text, Platform } from "react-native";
import { Constants, Location, Permissions } from "expo";
class PermissionsExample extends React.Component {
state = {
message: null,
location: null
};
async componentWillMount() {
try {
if (Platform.OS === 'android' && !Constants.isDevice) {
this.setState({ message: "This doesn't work on Android simulator!" });
return;
}
const { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== "granted") {
this.setState({ message: "Location permission was NOT granted!" });
return;
}
const location = await Location.getCurrentPositionAsync({});
this.setState({ location });
} catch (err) {
console.log(err);
}
}
render() {
let text = "Fetching location";
const { message, location } = this.state;
if (message !== null) {
text = message;
} else {
text = JSON.stringify(location);
}
return (
<View>
<Text>{text}</Text>
</View>
);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment