'Has anything changed regarding the background location service in Xamarin.ios?
I'm using Xamarin Forms to develop a solution that needs a permanent background service, which also uses locations. I've ticked background mode, fetch and always on, but the location updates become less frequent with time. After 20 mins of background processing, the location updates seem random, ranging from 5mins to 1h. Not sure what I'm doing wrong.
public class LocationManager
{
protected CLLocationManager locMgr;
public event EventHandler<LocationUpdatedEventArgs> LocationUpdated = delegate { };
public LocationManager()
{
locMgr = new CLLocationManager();
LocMgr.DesiredAccuracy = CLLocation.AccurracyBestForNavigation;
locMgr.PausesLocationUpdatesAutomatically = false;
locMgr.ActivityType = CLActivityType.AutomotiveNavigation;
locMgr.DistanceFilter = CLLocationDistance.FilterNone ;
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
this.locMgr.RequestAlwaysAuthorization();
}
if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
{
this.locMgr.AllowsBackgroundLocationUpdates = true;
}
LocMgr.StartUpdatingLocation();
}
public async void StartLocationUpdates()
{
if (CLLocationManager.LocationServicesEnabled)
{
if (LocMgr != null)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0))
{
LocMgr.LocationsUpdated += LocationsUpdated;
}
else
{
LocMgr.UpdatedLocation += LocationsNewUpdated;
}
LocMgr.StartUpdatingLocation();
LocMgr.Failed += (object sender, NSErrorEventArgs e) => {
Console.WriteLine(e.Error);
};
}
}
else
{
Console.WriteLine("Location services not enabled");
}
}
}
Solution 1:[1]
CLLocationManager provides many options for filtering and configuring location data, including the frequency of updates. In this example, set the DesiredAccuracy to update whenever the location changes by a meter. For more information on configuring location update frequency and other preferences, refer to the CLLocationManager Class Reference in the Apple documentation: https://developer.apple.com/documentation/corelocation/cllocationmanager
if (CLLocationManager.LocationServicesEnabled) {
//set the desired accuracy, in meters
LocMgr.DesiredAccuracy = 1;
LocMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) =>
{
// fire our custom Location Updated event
LocationUpdated (this, new LocationUpdatedEventArgs (e.Locations [e.Locations.Length - 1]));
};
LocMgr.StartUpdatingLocation();
}
Sample link: https://docs.microsoft.com/en-us/samples/xamarin/ios-samples/location/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
