Check if location services are allowed using native response

I am currently working on an application using native native, which requires location services for some of its functions. I am exploring if there is an easy way to check if the user is on or not, so I can display a different view if this is not the case. I did not find any results other than those related to iOS or Android. Has anyone else found a solution?

+8
source share
5 answers

I was able to solve this by showing an error message when the location could not be restored.

(error) => { this.setState({servicesOn: false}), console.log(error); }, 
+1
source

Use the “Respond to Android Android Location Services Custom Dialog” module for Android

https://www.npmjs.com/package/react-native-android-location-services-dialog-box

install

 npm install react-native-android-location-services-dialog-box --save 

Js

 import LocationServicesDialogBox from "react-native-android-location-services-dialog-box"; LocationServicesDialogBox.checkLocationServicesIsEnabled({ message: "Use Location ?", ok: "YES", cancel: "NO" }).then(function(success) { console.log(success); // success => "enabled" ).catch((error) => { console.log(error.message); // error.message => "disabled" }); 
+5
source

Take a watchPosition or getCurrentPosition, the second argument is the callback in case of failure, and its argument is the reason for its failure.

So, from there you know when and why it fails. You will need if if, for this reason, if you want to act only on a “disconnected location”, and not on a “wait time” request.

You can use a callback to add a flag to your state, for example.

0
source

You can also try these 2 libraries:

https://github.com/c19354837/react-native-system-setting

https://github.com/efkan/react-native-system-settings

I have the following: I get this error code 1 or the error message: "The location provider is missing." when my phone location service is disabled.

At another time, when it expires, I get this error code 3 or the error message: "The request for a placement has timed out."

Thus, based on the error code and the message, I show different toasts to inform the user.

You can refer to this doc position error on MDN

enter image description here

0
source

https://github.com/nearit/react-native-connectivity-status

Check condition

 import ConnectivityManager from 'react-native-connectivity-status' // Check if Location Services are enabled const locationServicesAvailable = await ConnectivityManager.areLocationServicesEnabled() 

You can also subscribe to updates

 import ConnectivityManager from 'react-native-connectivity-status' const connectivityStatusSubscription = ConnectivityManager.addStatusListener(({ eventType, status }) => { switch (eventType) { case 'bluetooth': console.log('Bluetooth is ${status ? 'ON' : 'OFF'}') break case 'location': console.log('Location Services are ${status ? 'AVAILABLE' : 'NOT available'}') break } }) 
0
source

All Articles