Reputation: 141
I'm looking for a simple way to know the GPS permission status on an iOS/Android device.
I'll give an example to clarify my question:
If the GPS on the device is turned off (so there is no way to obtain the longitude/latitude coordinates) I would display a label in this way.
<p *ngIf="!isGPSTurnedOn()">GPS is turned off, please go to settings and turn it on!<\p>
And in the ts file:
isGPSTurnedOn() { return .... }
There is also the possibility that the GPS is turned on but the location permission is not granted by the user. Something like this
<p *ngIf="!isGPSPermissionGranted()">Location permission is not granted. Please go to settings and grant it!<\p>
Same story in the ts file.
My final goal is to try to obtain GPS position if both permissions are granted: otherwise, the user will be warn about the issue.
Thanks in advance!
Upvotes: 1
Views: 488
Reputation: 2835
What you need is the Ionic Native - Diagnostic plugin.
https://ionicframework.com/docs/native/diagnostic/
With this plugin, you can check whether device features are enabled or not, GPS included.
Some useful methods (from plugin documentation):
isLocationEnabled()
:
Returns true if the device setting for location is on. On Android this returns true if Location Mode is switched on. On iOS this returns true if Location Services is switched on.
isLocationAuthorized()
:
Checks if the application is authorized to use location. Note for Android: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time.
getLocationAuthorizationStatus()
:
Returns the location authorization status for the application.
requestLocationAuthorization(mode)
:
Returns the location authorization status for the application. Note for Android: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time.
Upvotes: 3