应用程序在后台时如何检查位置?

时间:2014-05-06 08:27:53

标签: ios google-maps core-location

我正在尝试创建一个位置应用。您可以为特定位置设置警报。当您到达该位置时,将开始报警。 现在,问题是,当应用程序处于后台时,我如何检查我到达所需位置的天气? 根据我的知识,苹果允许任何代码在后台运行最多10分钟。 但是有许多应用程序可以在后台持续检查特定位置。

建议任何方法来实现这一点。

4 个答案:

答案 0 :(得分:1)

您可以使用region monitoring capability of the core-location framework

您可以将目的地定义为某个地区,然后即使您在后台,也会接到代理locationManager:didEnterRegion:方法的电话。

由于您没有主动执行,因此10分钟后台任务限制不适用。

答案 1 :(得分:0)

我会使用区域监控,然后在委托方法中调用

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region

答案 2 :(得分:0)

有两种方式。

  1. 首先在后台启用位置,在info.plist文件中添加值为UIBackgroundModes的键location。它现在允许您的应用程序甚至在应用程序进入后台10分钟后获取位置。因此,在每个新位置比较其距离与所需位置。如果它处于允许的限度内,请执行操作。
  2. 对于所需区域,您应该创建一个区域,并且应该使用位置管理器的委托开始观察这些区域。这将允许您的应用即使未处于运行模式也能自行启动。如果应用未运行,iOS将自动启动您的应用。

答案 3 :(得分:0)

  

认为 this 文章会对您有所帮助

您需要做的第一件事是修改应用程序的plist。添加以下设置。

应用程序不在后台运行:否

所需的背景模式:VOIP

并设置应用程序启动时的代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"Starting app");

    self.powerHandler = [[PowerHandler alloc] init];
    self.lastBatteryState = UIDeviceBatteryStateUnknown;

    UIApplication* app = [UIApplication sharedApplication];

    self.expirationHandler = ^{
        [app endBackgroundTask:self.bgTask];
        self.bgTask = UIBackgroundTaskInvalid;
        self.bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
        NSLog(@"Expired");
        self.jobExpired = YES;
        while(self.jobExpired) {
            // spin while we wait for the task to actually end.
            [NSThread sleepForTimeInterval:1];
        }
        // Restart the background task so we can run forever.
        [self startBackgroundTask];
    };
    self.bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];

    // Assume that we're in background at first since we get no notification from device that we're in background when
    // app launches immediately into background (i.e. when powering on the device or when the app is killed and restarted)
    [self monitorBatteryStateInBackground];

    return YES;
}

接下来,我们设置了后台工作

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"Entered background");
    [self monitorBatteryStateInBackground];
}

- (void)monitorBatteryStateInBackground
{
    NSLog(@"Monitoring battery state");
    self.background = YES;
    self.lastBatteryState = UIDeviceBatteryStateUnknown;
    [self startBackgroundTask];
}

- (void)startBackgroundTask
{
    NSLog(@"Restarting task");
    // Start the long-running task.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // When the job expires it still keeps running since we never exited it. Thus have the expiration handler
        // set a flag that the job expired and use that to exit the while loop and end the task.
        while(self.background && !self.jobExpired)
        {
            [self updateBatteryState:[self.powerHandler checkBatteryState]];
            [NSThread sleepForTimeInterval:1];
        }

        self.jobExpired = NO;
    });
}



- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */

    NSLog(@"App is active");
    self.background = NO;
}