Advertisement
Badal_hs_shah

PermissionHandler

Jan 24th, 2023 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {Alert, Platform} from 'react-native';
  2. import {
  3.   check,
  4.   openSettings,
  5.   PERMISSIONS,
  6.   request,
  7.   RESULTS,
  8. } from 'react-native-permissions';
  9. import Geolocation, {GeoCoordinates} from 'react-native-geolocation-service';
  10. import DeviceInfo from 'react-native-device-info';
  11. import {setItemInStorage} from './Storage';
  12. import {t} from 'i18next';
  13.  
  14. const PermissionHandler = {
  15.   openSetting: (page = 'other') => {
  16.     var title =
  17.       page === 'ischeckout'
  18.         ? 'Necesitamos su permiso de ubicación para un mejor servicio. Permita que se configure.'
  19.         : '¿Quieres cambiar el permiso ?. es decir, servicio de ubicación.';
  20.  
  21.     Alert.alert('', title, [
  22.       {
  23.         text: 'Cancel',
  24.         onPress: () => {},
  25.         style: 'cancel',
  26.       },
  27.       {text: 'Go to Setting', onPress: () => openSettings()},
  28.     ]);
  29.   },
  30.  
  31.   getCurrentLatLong: () => {
  32.     try {
  33.       Geolocation.getCurrentPosition(
  34.         position => {
  35.           // console.log('FN getCurrentLatLong  ---- ', position.coords );
  36.           setItemInStorage('latitude', position.coords.latitude.toString());
  37.           setItemInStorage('longitude', position.coords.longitude.toString());
  38.           return position.coords;
  39.         },
  40.         error => {
  41.           // See error code charts below.
  42.           console.log('getCurrentLatLong  ---- ', error.code, error.message);
  43.         },
  44.         {
  45.           enableHighAccuracy: true,
  46.           timeout: 10000,
  47.           maximumAge: 10000,
  48.         },
  49.       );
  50.     } catch (error) {
  51.       return null;
  52.     }
  53.   },
  54.  
  55.   hasLocationPermission: async () => {
  56.     if (Platform.OS === 'ios') {
  57.       const hasPermission = await hasPermissionIOS();
  58.       return hasPermission;
  59.     }
  60.  
  61.     if (Platform.OS === 'android' && Platform.Version < 23) {
  62.       return true;
  63.     }
  64.  
  65.     const hasPermission = await check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION);
  66.  
  67.     if (hasPermission) {
  68.       return true;
  69.     }
  70.  
  71.     const status = await request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION);
  72.  
  73.     if (status === RESULTS.GRANTED) {
  74.       return true;
  75.     }
  76.  
  77.     return false;
  78.   },
  79. };
  80.  
  81. export default PermissionHandler;
  82.  
  83. async function hasPermissionIOS() {
  84.   const status = await Geolocation.requestAuthorization('always');
  85.   console.log('hasPermissionIOS --- ', status);
  86.  
  87.   if (status === 'granted') {
  88.     return true;
  89.   }
  90.  
  91.   if (status === 'denied' || status === 'disabled' || status === 'restricted') {
  92.     Alert.alert(
  93.       `${t('LOCATION_PERMISSION_DENIED_TITLE')}`,
  94.       `${t('LOCATION_PERMISSION_DENIED_MESSAGE')}`,
  95.  
  96.       [
  97.         {
  98.           text: `${t('SETTING')}`,
  99.           onPress: () => {
  100.             openSettings();
  101.           },
  102.         },
  103.         {
  104.           text: `${t('TRY_AGAIN')}`,
  105.           onPress: async () => {
  106.             TryAgain();
  107.           },
  108.         },
  109.       ],
  110.     );
  111.   }
  112.  
  113.   return false;
  114. }
  115.  
  116. async function TryAgain() {
  117.   const hasPermission = await PermissionHandler.hasLocationPermission();
  118.   if (hasPermission) {
  119.     //Call function from anyclass here
  120.   }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement