3

我正在为 iOS 5 编写一个基于位置的应用程序。该应用程序正在使用CLLocationManager接收位置更新。

当应用程序启动时,我正在创建 LocationManager,调用[self.locationManager startUpdatingLocation];并且灰色的位置箭头出现在状态栏中。

当应用程序进入后台时,我正在调用

[self.locationManager stopUpdatingLocation]; 
[self.locationManager startMonitoringSignificantLocationChanges];

因为我只想在应用程序处于后台时接收重大的位置更改。灰色的位置箭头仍然显示。

我在调用的应用程序中也有一个退出按钮

[self.locationManager stopUpdatingLocation];
exit(0);

当用户按下退出按钮时,应用程序完成,灰色的位置按钮从状态栏中消失。直到现在一切都很好。

当用户使用 Fast App Switcher 完成应用程序时,会出现一个问题,因为之后状态栏中仍然显示灰色的位置箭头,尽管应用程序没有运行。当用户从他的 iPhone 中删除应用程序时,灰色的位置箭头首先消失。

所以问题是:

  • 当用户使用 Fast App Switcher 完成应用程序并且状态栏中仍显示灰色箭头时,这是否意味着位置更新仍在执行,或者这是否意味着应用程序在过去 24 小时内使用了位置服务?我猜答案是第一个,位置更新仍在执行!

  • 应用程序需要在后台执行,所以当应用程序进入后台时我无法停止 LocationManager。applicationWillTerminate:当用户使用 Fast App Switcher 终止应用程序时,永远不会调用。当应用程序使用快速应用程序切换器终止或被操作系统终止时,有什么方法可以接收事件????

  • 我见过其他一些应用程序,当应用程序使用 Fast App Switcher 终止时,状态栏中的灰色箭头会消失。这些应用程序是如何实现这一点的???

4

2 回答 2

3
  1. 如果您的应用不在前台,则可以使用重要的位置更新。
  2. 我只是给你一个想法,我做过这样的事情,但我不知道你的情况会发生什么。
    只需将您的应用程序注册为后台任务并为测试目的做一些事情,在这种情况下,如果您通过快速应用程序切换杀死您的应用程序,applicationWillTerminate:则始终调用委托方法。


_ __ _ __ _ __ _ _已编辑_ __ _ __ _ __ _ __ _ _
只需将此变量添加到您的 AppDelegate.h

UIBackgroundTaskIdentifier bgTask;

让你的委托方法像这样,现在杀死你的应用程序

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        UIApplication *app = [UIApplication sharedApplication];
        if ([app respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)]) {
            bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
                // Synchronize the cleanup call on the main thread in case
                // the task actually finishes at around the same time.
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (bgTask != UIBackgroundTaskInvalid)
                    {
                        [app endBackgroundTask:bgTask];
                        bgTask = UIBackgroundTaskInvalid;
                    }
                });
            }];
        }
    }

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSLog(@"is terminated");
}


_ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _

于 2012-01-31T09:43:14.937 回答
3

尝试在“设置->常规->重置->重置位置警告”中恢复“位置警告”。它使用“startupdatinglocation”对我有用。

于 2012-06-10T08:54:11.270 回答