-
-
Notifications
You must be signed in to change notification settings - Fork 76
Conversation
Implements #182 This is an optional setting for the consumer to pass when calling I've mentioned this on the camera plugin, and plan to implement the same flow in a PR shortly as well. @tbozhikov liked the suggestion there. I've ran into a situation where I'd like to open the settings is the enabled check resolves false and this. |
Just realized currently android has options for |
Okay, made the change necessary. So now the |
Added doc on the typings about iOS using the new value for the |
Working more on this UX flow today in an app and even this would sometimes be the wrong approach. Scenario: Only when the status is I'm going to rethink this PR a bit and see if I can provide a more elegant approach without breaking anything. |
@lini @tbozhikov - not sure who is doing most of the work on this plugin anymore but know you're both contributors 😄. This PR has changed a bit since I first created it so I want to recap the status and why so you're aware of the details. I have an app where I've been getting logs with So I went digging and my use case was fairly standard, check if the geolocation was enabled and then try to get the current location. Of course if the user has previously denied the Location permission prompt iOS provides, nothing happens. So this PR is providing two separate things:
I believe everything has been documented on the README and that this is not a breaking change since android remains the same and only iOS has been altered with a new configuration option on the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution @bradmartin! I've made some in-line comments.
In general, I think that the enableLocationRequest()
method is a better place to put the logic for opening the Settings app to enable Location services by hand. I am pasting code here since I cannot commit directly in your repo:
- geolocation.ios.ts
export function enableLocationRequest(always?: boolean, iosOpenSettingsIfLocationHasBeenDenied?: boolean): Promise<void> {
return new Promise<void>(function (resolve, reject) {
const locationIsEnabled = _isEnabled();
if (locationIsEnabled) {
resolve();
return;
} else {
const status = getIOSLocationManagerStatus();
if (status === CLAuthorizationStatus.kCLAuthorizationStatusDenied &&
iosOpenSettingsIfLocationHasBeenDenied) {
// now open the Settings so the user can toggle the Location permission
utils.ios.getter(UIApplication, UIApplication.sharedApplication).openURL(NSURL.URLWithString(UIApplicationOpenSettingsURLString));
} else {
let listener = LocationListenerImpl.initWithPromiseCallbacks(resolve, reject, always);
try {
let manager = getIOSLocationManager(listener, null);
if (always) {
manager.requestAlwaysAuthorization();
} else {
manager.requestWhenInUseAuthorization();
}
} catch (e) {
LocationMonitor.stopLocationMonitoring(listener.id);
reject(e);
}
}
}
});
}
... and below is how I imagine the other 3 methods would look like:
function _isEnabled(): boolean {
if (CLLocationManager.locationServicesEnabled()) {
const status = getIOSLocationManagerStatus();
// CLAuthorizationStatus.kCLAuthorizationStatusAuthorizedWhenInUse and
// CLAuthorizationStatus.kCLAuthorizationStatusAuthorizedAlways are options that are available in iOS 8.0+
// while CLAuthorizationStatus.kCLAuthorizationStatusAuthorized is here to support iOS 8.0-.
// const AUTORIZED_WHEN_IN_USE = CLAuthorizationStatus.kCLAuthorizationStatusAuthorizedWhenInUse;
return (status === CLAuthorizationStatus.kCLAuthorizationStatusAuthorizedWhenInUse
|| status === CLAuthorizationStatus.kCLAuthorizationStatusAuthorizedAlways
|| status === CLAuthorizationStatus.kCLAuthorizationStatusAuthorized);
}
return false;
}
export function isEnabled(options: Options): Promise<boolean> {
return new Promise(function (resolve, reject) {
const isEnabledResult = _isEnabled();
resolve(isEnabledResult);
});
}
export function getIOSLocationManagerStatus(): CLAuthorizationStatus {
return CLLocationManager.authorizationStatus();
}
- main-page.ts
add the second parameter toenableLocationRequest()
call:
geolocation.enableLocationRequest(true, true)
[optional] Also, you may mention in the README, that the iosOpenSettingsIfLocationHasBeenDenied
option is an argument in the enableLocationRequest()
method. If you don't I will add this later.
What do you think of these changes?
return new Promise(function (resolve, reject) { | ||
resolve(_isEnabled()); | ||
const isEnabledResult = _isEnabled(); | ||
const status = CLLocationManager.authorizationStatus(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could reuse the getIOSLocationManagerStatus()
, i.e.:
const status = CLLocationManager.authorizationStatus();
@@ -76,6 +76,8 @@ geolocation.getCurrentLocation({ desiredAccuracy: Accuracy.high, maximumAge: 500 | |||
| timeout | 5 minutes | How long to wait for a location in ms. | | |||
| iosAllowsBackgroundLocationUpdates | false | If enabled, UIBackgroundModes key in info.plist is required (check the hint below). Allow the application to receive location updates in background (ignored on Android). Read more in [Apple document](https://developer.apple.com/documentation/corelocation/cllocationmanager/1620568-allowsbackgroundlocationupdates?language=objc) | | |||
| iosPausesLocationUpdatesAutomatically | true | Allow deactivation of the automatic pause of location updates (ignored on Android). Read more in [Apple document](https://developer.apple.com/documentation/corelocation/cllocationmanager/1620553-pauseslocationupdatesautomatical?language=objc)| | |||
| iosOpenSettingsIfLocationIsDisabled | false | If true when the `isEnabled` method is invoked, the settings app will open on iOS so the user can change the location services permission. | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iosOpenSettingsIfLocationIsDisabled -> iosOpenSettingsIfLocationHasBeenDenied
I like this better than using Thanks for reviewing it. |
new PR with some tweaks added -> #185, good job @bradmartin! |
No description provided.