dpa99c / cordova-plugin-request-location-accuracy

Cordova/Phonegap plugin for Android and iOS to request enabling/changing of Location Services by triggering a native dialog
96 stars 62 forks source link

Success method is called before user grant the permission. #39

Closed nisos-siddharth closed 7 years ago

nisos-siddharth commented 7 years ago

I am developing the ionic2 app in these I want to use GPS location. I used following code

this.locationAccuracy.canRequest().then((canRequest: boolean) => { if (canRequest) { this.locationAccuracy.request(this.locationAccuracy.SUCCESS_USER_AGREED).then( (res) => { this.geolocation.getCurrentPosition().then((resp) => { console.log("getCurrentPosition", resp); this.coordinates = resp.coords; }).catch((error) => { console.log('Error getting location', error); }); }, error => console.log('Error requesting location permissions', error) ); } });

In this code, before the user grants the permission of location, the getCurrentPosition() method is called and then this method is unable to access GPS location. How to make this wait until the user grants or denies the permission?

dpa99c commented 7 years ago

You are erroneously passing a success callback constant - locationAccuracy.SUCCESS_USER_AGREED] as the request argument instead of a request constant.

i.e your call should be something like:

this.locationAccuracy.request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY).then((res) => {
    if(res === this.locationAccuracy.SUCCESS_SETTINGS_SATISFIED){
        //etc.
    }
});

Please see the example project for an example of how the plugin should be used.

nisos-siddharth commented 7 years ago

I tried your code, but this also runs the success function before user gives the permission. Can we wait for the execution of this method until user grants the permission?

dpa99c commented 7 years ago

Since you're talking about GPS location, I assume you are referring to the Android platform?

As can be seen in the native Android code here and here, the plugin waits for the outcome of the permission request activity before invoking the success callback. However, you are using it via the Ionic Native wrapper, not directly so maybe Ionic Native is invoking the success callback directly.

You could try calling the plugin directly:

cordova.plugins.locationAccuracy.request(function (success){
    console.log("Successfully requested accuracy: "+success.message);
}, function (error){
   console.error("Accuracy request failed: error code="+error.code+"; error message="+error.message);
}, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY);
nisos-siddharth commented 7 years ago

I am developing the hybrid app so its runs on Android as well as Ios platform.

dpa99c commented 7 years ago

I am developing the hybrid app so its runs on Android as well as Ios platform.

Which platform are you reporting this issue against: Android or iOS? I assume Android, because the iOS behaviour is different (i.e. shows dialog to Location Settings)

nisos-siddharth commented 7 years ago

I am reporting for both Android as well as Ios. On both platforms, success function is called before granting the permission.

dpa99c commented 7 years ago

iOS works completley differently from Android: it implicitly displays the "Settings" dialog, which is part of the iOS OS, by requesting a location. The location request immediately fails due to Location Services being OFF, so the location error handler is immediately invoked (i.e. concurrently with the display of the Settings dialog). Hence the success callback for iOS is invoked immediately: there is no asynchronous trigger which can be used to invoke it. While in theory, the plugin could wait for a successful location, this would mean the success callback would never be called if the user pressed "Cancel" in the dialog or if a location could not be returned by the Location Manager (e.g. no cellular/GPS signal).

However, as I've pointed out above, Android does wait for the asynchronous outcome of the request dialog, as can be seen in the native code.

dpa99c commented 7 years ago

I would suggest debugging your Android app in Android Studio and placing breakpoints on the native code points I've highlighted above, to confirm that the Android implementation does wait for the asynchronous outcome of the permissions activity (i.e. the user's decision).

nisos-siddharth commented 7 years ago

Thanks, It's working now.