Ios 若用户在应用程序处于后台时进入/退出区域,则调用本地通知

Ios 若用户在应用程序处于后台时进入/退出区域,则调用本地通知,ios,objective-c,xcode,cllocationmanager,Ios,Objective C,Xcode,Cllocationmanager,如果用户在应用程序处于后台模式时进入或退出某个区域,我需要执行调用LocalNofication的简单任务。只有一组坐标会触发通知。例如: 拉丁美洲:41.8500 伦敦:87.6500 半径:300 我知道如何调用localNotification,以及如何使用locationManager的基本功能,但似乎无法在后台跟踪位置。任何帮助都会很好 请查看beginBackgroundTaskWithExpirationHandler:的方法。它允许您在应用程序处于后台时请求额外的时间来运行任务

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

拉丁美洲:41.8500 伦敦:87.6500 半径:300


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

请查看beginBackgroundTaskWithExpirationHandler:的方法。它允许您在应用程序处于后台时请求额外的时间来运行任务

有关更多信息,我建议您阅读本手册的“后台执行和多任务处理”部分。它详细解释了当应用程序进入后台时会发生什么,以及你可以做什么

具体来说,它显示了当应用程序在后台运行时运行长任务的示例代码:

[此代码取自上面链接的Apple指南]


请查看的
beginBackgroundTaskWithExpirationHandler:
方法。它允许您在应用程序处于后台时请求额外的时间来运行任务

有关更多信息,我建议您阅读本手册的“后台执行和多任务处理”部分。它详细解释了当应用程序进入后台时会发生什么,以及你可以做什么

具体来说,它显示了当应用程序在后台运行时运行长任务的示例代码:

[此代码取自上面链接的Apple指南]


您是否已阅读了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];
之后,设备将监控指定区域的入口/出口,即使你的应用程序在后台。当跨越某个区域时,代理将收到一个呼叫
locationManager:didEnterRegion:
locationManager:didExitRegion:
。您可以利用此机会发布
UILocalNotification
。如果您的应用程序在跨区域时未运行,它将在后台启动,您需要在
应用程序:didfishlaunchingwithoptions:
中查找相应的键。使用如下代码:

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秒的时间在后台运行。

您是否阅读了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];
之后,设备将监控指定区域的入口/出口,即使你的应用程序在后台。当跨越某个区域时,代理将收到一个呼叫
locationManager:didEnterRegion:
locationManager:didExitRegion:
。您可以利用此机会发布
UILocalNotification
。如果您的应用程序在跨区域时未运行,它将在后台启动,您需要在
应用程序:didfishlaunchingwithoptions:
中查找相应的键。使用如下代码:

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秒的时间在后台运行。

似乎是在检测到UIApplicationLaunchActionsLocationKey时创建位置管理器并设置代理不足以获取触发应用程序基于位置启动的数据。似乎是在检测到UIApplicationLaunchActionsLocationKey时创建位置管理器并设置代理UIApplicationLaunchActionsLocationKey不足以获取触发应用程序基于位置启动的数据。