Xamarin iOS - 在终止应用程序后处理地理围栏交叉点触发器

Xamarin iOS - Handling Geofence intersection triggers after terminating an app

我正在尝试在我的应用程序终止(不是后台)时处理地理围栏交叉点触发器。我想处理来自 CLLocationManager 的 enter/exit 触发器,即使我的应用不是 运行。它应该在后台唤醒我的应用程序,在 enter/exit 上执行所需的处理。

要做到这一点,很多地方都可以使用后台应用程序刷新功能。我编写了以下代码,但一旦我终止应用程序,它就会停止听到地理围栏触发事件。

任何人都可以指导我如何处理这些事件,即使应用程序已终止?

 public async Task StartLocationUpdates()
    {
        _cts = new CancellationTokenSource();
        _taskId = UIApplication.SharedApplication.BeginBackgroundTask("LongRunningTask", OnExpiration);

        try
        {
            if (CLLocationManager.LocationServicesEnabled)
            {
                LocMgr.DesiredAccuracy = 1;

                LocMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) =>
                {
                    var locations = e.Locations;                       
                };
                LocMgr.StartUpdatingLocation();

                if (CLLocationManager.LocationServicesEnabled)
                {

                    if (CLLocationManager.Status != CLAuthorizationStatus.Denied)
                    {

                        if (CLLocationManager.IsMonitoringAvailable(typeof(CLCircularRegion)))
                        {

                            LocMgr.DidStartMonitoringForRegion += (o, e) =>
                            {
                                Console.WriteLine("Now monitoring region {0}", e.Region.ToString());
                            };

                            LocMgr.RegionEntered += (o, e) =>
                            {
                                Instance.Speak("Just entered " + e.Region.ToString());
                            };

                            LocMgr.RegionLeft += (o, e) =>
                            {
                                Instance.Speak("Just left " + e.Region.ToString());
                            };
                            foreach (CLCircularRegion region in RegionList)
                            {
                                if (region != null)
                                {
                                    StopMonitoringRegion(region);
                                }
                                LocMgr.StartMonitoring(region);
                            }
                        }
                        else
                        {
                            Console.WriteLine("This app requires region monitoring, which is unavailable on this device");
                        }

                        LocMgr.Failed += (o, e) =>
                        {
                            Console.WriteLine(e.Error);
                        };

                    }
                    else
                    {
                        Console.WriteLine("App is not authorized to use location data");
                    }

                }
                else
                {
                    Console.WriteLine("Location services not enabled, please enable this in your Settings");
                }


            }
        }
        catch (OperationCanceledException)
        {
        }
    }

提前谢谢你。

您似乎已经在 info.plist 中添加了私钥以启用定位服务。如果你想在应用程序处于后台状态或终止时启用它,我们还应该打开后台模式:功能>后台模式>位置更新。

然后我们可以在AppDelegate中初始化位置管理器:

CLLocationManager locationManager;
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
    locationManager = new CLLocationManager();
    locationManager.Delegate = new MyLocationDelegate();
    //enable service when app is on background or terminated
    locationManager.PausesLocationUpdatesAutomatically = false;
    locationManager.AllowsBackgroundLocationUpdates = true;
    locationManager.RequestAlwaysAuthorization();
    locationManager.DistanceFilter = 100.0;
    locationManager.DesiredAccuracy = 1;
    // The important request can awake your app
    locationManager.StartMonitoringSignificantLocationChanges();
    locationManager.StartUpdatingLocation();

    return true;
}

即使您的应用程序已终止,如果您的位置已更改,事件 LocationsUpdated() 也会触发:

public class MyLocationDelegate : CLLocationManagerDelegate
{
    public override void LocationsUpdated(CLLocationManager manager, CLLocation[] locations)
    {
        //Handle your method here
    }
}

您可以使用模拟器进行测试:调试 > 位置 > 高速公路行驶。在上述事件中将位置存储到您的沙箱文件夹中。强制退出您的应用程序,几秒钟后打开它,检索您存储的文件。您会找到整个位置记录。