Get promise of the result of activating geolocation
Closed this issue · 5 comments
if (ionic.Platform.isIOS()) {
cordova.plugins.diagnostic.switchToSettings();
} else {
cordova.plugins.diagnostic.switchToLocationSettings();
}
I can open the native device configuration to mark the geolocation. but I do not know how to do it when the user turns on the geolocation, it simply executes that code but does not get an answer from that. I would like (if possible) to know if the user turned on the geolocation or not.
how can I do it?
The "switchToSettings"-type functions cause the native iOS or Android system app to be brought into the foreground, hence your Cordova app goes into the background and its execution paused, therefore a promise or callback cannot be passed to these functions as it would never be invoked.
Instead, you should use getLocationAuthorizationStatus() to check the current location settings when your app resumes from the background.
You can also use registerLocationStateChangeHandler() to detect changes made to the location settings while the app is running in the foreground e.g. via Android Quick Settings or iOS Task Manager.
@dpa99c
Sorry, I'm new to the topic, I'm very touched by your long and good answers. in my case I want to verify if the user turned on the gps from the native configuration. should I include some function like,
document.addEventListener("resume", function() {
getLocationAuthorizationStatus()..
}, false);
or does this function do it registerLocationStateChangeHandler() automatically?
thanks.. it's an honor for me to receive your answers
Yes, you should do both: registerLocationStateChangeHandler()
will only be invoked while your app is running in the foreground, so you should also check with getLocationAuthorizationStatus()
when your app resumes from the background, in case the user changed it while your app was paused. And of course also check getLocationAuthorizationStatus()
on app startup (deviceready
).
Managing location settings is tricky on Android or iOS, so it gets really hard when you are try to do both at the same time 😀
thanks, in android I can go directly to the settings to activate the geolocation with
cordova.plugins.diagnostic.switchToLocationSettings();
in ios does not work with
this:
cordova.plugins.diagnostic.switchToSettings();
what can I do?
On iOS, it's not possible to directly open the Privacy page in the Settings app (which contains the Location Services switch) via an API call.
Well, actually it is, but doing so requires calling a private API function that will get your app rejected from the App Store.
The purpose of this plugin implementation for iOS is, if Location Services is OFF, then you can trigger a native iOS dialog that will directly open the Privacy page so the user can turn Location Services ON (they have to manually flip the switch - it cannot be done programmatically).
See the iOS overview section.