jamesmontemagno / Xamarin.Plugins

Cross-platform Native API Access from Shared Code!
MIT License
1.3k stars 380 forks source link

Have IsGeolocationAvailable and IsGeolocationEnabled take into account permissions #300

Closed ferrydeboer closed 8 years ago

ferrydeboer commented 8 years ago

Please take a moment to fill out the following (change to preview to check or place x in []):

This is a

Which plugin does this impact:

Version Number of Plugin: 3.0.4 Device Tested On: Sony Xperia Z3 Compact Simulator Tested On: -

Expected Behavior

Plugin will request permission when IsGeolocationAvailable or IsGeolocationEnabled is queried but there are no granted permissions yet.

Actual Behavior

We query both properties prior to doing a location request thus preventing a possible exception. But since these properties return false when no permissions have been granted yet our code never reaches the GetPositionAsync where the actual permission is requested.

Steps to reproduce the Behavior

Change lines 29 and 30 of GeolocatorTests.GeolocatorTests.cs to

if (CrossGeolocator.Current.IsGeolocationAvailable && CrossGeolocator.Current.IsGeolocationEnabled)
{
    var test = await CrossGeolocator.Current.GetPositionAsync();
    label.Text = "Lat: " + test.Latitude.ToString() + " Long: " + test.Longitude.ToString();
}
else
{
    label.Text = "GPS is unavailable or not enabled!";
}
jamesmontemagno commented 8 years ago

Hmmm, this is a tricky one as to find this out you need to have it be a Task

Providers will also have a problem if permission isn't requested....

Need to think about this and how we can change the API.

ferrydeboer commented 8 years ago

I use both booleans in conjunction. What about an extra function Task<bool> AssureGpsAvailable(). This first checks/requests permissions and only then queries the booleans. That way the existing API doesn't need to be changed.

You can query the booleans if you want to inform the user more specifically in case the Assure function returns false. If both booleans are then true it's sort of safe to assume the AssureGpsAvailable returned false because of the permissions. Or you can return an enum from AssureGpsAvailable instead of a boolean.

jamesmontemagno commented 8 years ago

until I have a fix checkout using the permissions plugin for this :)

jamesmontemagno commented 8 years ago

This issue was moved to jamesmontemagno/GeolocatorPlugin#4