问题
I am new to react native . I want to ask real time permission for Location .Please help me how to take Real Time Permission in react native.
回答1:
Best avoid libraries for permission check
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Important - include the above code line in your manifest file
import { PermissionsAndroid, Platform } from 'react-native';
componentDidMount() {
if (Platform.OS === 'android') {
this.requestLocationPermission(); // function call
}
}
Below code
contains function
body.
async requestLocationPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
);
if (granted === PermissionsAndroid.RESULTS.GRANTED || granted === true)
{
// WHAT TODO WHEN PERMISSION IS GRANTED
} else {
// WHAT TODO WHEN PERMISSION IS NOT GRANTED
}
} catch (err) {
log(err);
}
}
回答2:
take a look at this library: react-native-permissions
回答3:
react-native-permissions might be overkill if you only need access to location. React Native seems to have its own API for that : RN geolocation
import {geolocation } from "react-native";
// ...
geolocation.requestAuthorization()
Permission required on iOS depends on which keys you have in your plist. For android, I guess you have nothing to do
来源:https://stackoverflow.com/questions/52701561/real-time-permission-in-react-native