Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {Alert, Platform} from 'react-native';
- import {
- check,
- openSettings,
- PERMISSIONS,
- request,
- RESULTS,
- } from 'react-native-permissions';
- import Geolocation, {GeoCoordinates} from 'react-native-geolocation-service';
- import DeviceInfo from 'react-native-device-info';
- import {setItemInStorage} from './Storage';
- import {t} from 'i18next';
- const PermissionHandler = {
- openSetting: (page = 'other') => {
- var title =
- page === 'ischeckout'
- ? 'Necesitamos su permiso de ubicación para un mejor servicio. Permita que se configure.'
- : '¿Quieres cambiar el permiso ?. es decir, servicio de ubicación.';
- Alert.alert('', title, [
- {
- text: 'Cancel',
- onPress: () => {},
- style: 'cancel',
- },
- {text: 'Go to Setting', onPress: () => openSettings()},
- ]);
- },
- getCurrentLatLong: () => {
- try {
- Geolocation.getCurrentPosition(
- position => {
- // console.log('FN getCurrentLatLong ---- ', position.coords );
- setItemInStorage('latitude', position.coords.latitude.toString());
- setItemInStorage('longitude', position.coords.longitude.toString());
- return position.coords;
- },
- error => {
- // See error code charts below.
- console.log('getCurrentLatLong ---- ', error.code, error.message);
- },
- {
- enableHighAccuracy: true,
- timeout: 10000,
- maximumAge: 10000,
- },
- );
- } catch (error) {
- return null;
- }
- },
- hasLocationPermission: async () => {
- if (Platform.OS === 'ios') {
- const hasPermission = await hasPermissionIOS();
- return hasPermission;
- }
- if (Platform.OS === 'android' && Platform.Version < 23) {
- return true;
- }
- const hasPermission = await check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION);
- if (hasPermission) {
- return true;
- }
- const status = await request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION);
- if (status === RESULTS.GRANTED) {
- return true;
- }
- return false;
- },
- };
- export default PermissionHandler;
- async function hasPermissionIOS() {
- const status = await Geolocation.requestAuthorization('always');
- console.log('hasPermissionIOS --- ', status);
- if (status === 'granted') {
- return true;
- }
- if (status === 'denied' || status === 'disabled' || status === 'restricted') {
- Alert.alert(
- `${t('LOCATION_PERMISSION_DENIED_TITLE')}`,
- `${t('LOCATION_PERMISSION_DENIED_MESSAGE')}`,
- [
- {
- text: `${t('SETTING')}`,
- onPress: () => {
- openSettings();
- },
- },
- {
- text: `${t('TRY_AGAIN')}`,
- onPress: async () => {
- TryAgain();
- },
- },
- ],
- );
- }
- return false;
- }
- async function TryAgain() {
- const hasPermission = await PermissionHandler.hasLocationPermission();
- if (hasPermission) {
- //Call function from anyclass here
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement