Created
January 16, 2019 05:36
-
-
Save aloukissas/161d9637f985cb0cfe6103cf5f5c8247 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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