Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Background location listening with Geolocator Plugin on iOS and permission When in use

admvieiraadmvieira Member ✭✭

Following the plugin's documentation, I implemented a foreground service on Android to obtain the user's location whenever the app is in the background.

For iOS the plugin has implementation for the background in StartListeningAsync but only works if the user grants permission "always".

After iOS 11, it does not display the request to access the location permission "always" when I call the RequestPermissionsAsync method (Permision.LacationAlways). First the system displays a request for location "WhenInUse" and only after putting the app in the background and returning to the app that the system will display the request "always".

This behavior makes my app not work the first time it is put in the background.

I could send the user to the app's location settings screen but this is no longer allowed by Apple, and sending them to the app's general settings screen is a bad experience for the user.

The ideal behavior would be to request "Always" right from the start or something like what happens in STRAVA (walking tracking app).

In the STRAVA app, when placed in the background, a notification appears at the top saying "STRAVA using your location actively" and continues using the location with the "WhenInUse" permission.

How should I proceed please?

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Apple now only provides three options(when in usage, allow once and don't allow) on iOS 13. Always allow permission has been removed from the permission window we can't let the user force choosing that at the initial time.
    If the user continues to use your application Apple will automatically and periodically display another window which always allow is located.
    Apple is in charge of this process and we can't change this default behavior.
    But first of all, background location mode needs to be checked on and NSLocationAlwaysAndWhenInUseUsageDescription
    has been added in the info.plist.

  • admvieiraadmvieira Member ✭✭

    Thanks for the answer LandLu.

    I have in my info.plist background location mode and NSLocationAlwaysAndWhenInUseUsageDescription.

    The behavior you described is what happens with my app today. But before Apple requests use always the app does not work in the background.

    I know it is possible to get the location in the background even if the user allows it when in use, but I don't know how to do it using Geolocator Plugin of James Montemagno.

  • LandLuLandLu Member, Xamarin Team Xamurai

    But before Apple requests use always the app does not work in the background.

    This is what happens on iOS 13 now. We can't let the user choose always allow at the initial time. And the background location can't be retrieved until the user chooses always allow on the popup window form the system.
    Apple says here: https://developer.apple.com/documentation/corelocation/choosing_the_location_services_authorization_to_request

    You must always be prepared to run with When In Use authorization.

  • admvieiraadmvieira Member ✭✭

    I read this documentation, it says:

    "Prefer When In Use Authorization
    Whenever possible, request only When In Use authorization. This mode has powerful features, allowing your app to:

    Access all available location services while the user is using the app. If the user stops using your app, any outstanding requests suspend until the user resumes using your app.

    Continue to get location updates even after your app enters the background, if you’ve enabled background location updates in your Xcode project. See Handling Location Events in the Background for more information."

    And as I mentioned, I use an app called STRAVA that gets my location in the background asking only when in use and My iphone OS is 13.3.1. So I would like to find out how to do this using the James Plugin.

    See below images from the STRAVA app.


  • admvieiraadmvieira Member ✭✭

    I think I will have to do without the Plugin, because if the user chooses only when in use the plugin fails to run the StartListeningAsync.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Did you change the AllowBackgroundUpdates and PauseLocationUpdatesAutomatically settings of the ListenerSettings?
    You could try to use the native method to retrieve the locations on iOS through dependency service:
    https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-walkthroughs/location-walkthrough

  • admvieiraadmvieira Member ✭✭

    Yes I changed the AllowBackgroundUpdates and PauseLocationUpdatesAutomatically methods.
    I also think that I will have to implement using native method.
    This is sad, because for Android the plugin served me very well.

  • LandLuLandLu Member, Xamarin Team Xamurai

    I start to use Essentials to implement the location service:
    https://docs.microsoft.com/en-us/xamarin/essentials/geolocation?context=xamarin/xamarin-forms&tabs=ios
    But it hasn't supported background location tracking now. And I don't know whether geolocation plugin supported the background location on iOS 13.
    It's better to try the native background location tracking as alternatively.

Sign In or Register to comment.