gpt4 book ai didi

ios - 如果用户在应用程序处于后台时进入/退出区域,则调用 localNotification

转载 作者:行者123 更新时间:2023-11-28 18:38:42 26 4
gpt4 key购买 nike

如果用户在应用程序处于后台模式时进入或退出区域,我需要执行调用 localNoification 的简单任务。只有一组坐标会触发通知。例如:

纬度:41.8500经度:87.6500半径:300

我知道如何调用 localNotification,以及如何使用 locationManager 的基本功能,但似乎无法在后台跟踪位置。任何帮助都会很棒!

最佳答案

您是否阅读过 CLLocationManager 的 startMonitoringForRegion: 方法?我认为这将完全符合您的要求。设置它的代码看起来像这样:

CLRegion * region = [[CLRegion alloc] initCircularRegionWithCenter: CLLocationCoordinate2DMake(41.8500, 87.6500) radius: 300 identifier: @"regionIDstring"];
CLLocationManager * manager = [[CLLocationManager alloc] init];
[manager setDelegate: myLocationManagerDelegate];
[manager startMonitoringForRegion: region];

之后,设备将监控指定区域的入口/导出,即使您的应用程序处于后台也是如此。当跨越一个区域时,委托(delegate)将收到一个调用 locationManager:didEnterRegion:locationManager:didExitRegion:。您可以利用这个机会发布 UILocalNotification。如果您的应用程序在跨区域时未运行,它将在后台启动,您需要在 application: didFinishLaunchingWithOptions: 中寻找合适的键。使用如下代码:

if ([launchOptions objectForKey: UIApplicationLaunchOptionsLocationKey] != nil) {
// create a location manager, and set its delegate here
// the delegate will then receive the appropriate callback
}

请注意,应用程序在后台运行时执行时间很短(几秒钟);如果您需要执行更长的任务,请在您的应用收到区域交叉通知后立即调用 Nebs 在他/她的回答中提到的 beginBackgroundTaskWithExpirationHandler: 方法。这将为您提供大约 600 秒的后台运行时间。

关于ios - 如果用户在应用程序处于后台时进入/退出区域,则调用 localNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14430407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com