尽pipe基于位置的应用程序使用快速应用程序切换器杀死,但状态栏中显示灰色GPS箭头

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

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

当应用程序进入后台我打电话

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

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

我在我的应用程序中也有一个退出button

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

当用户按下“退出”button时,应用程序结束,灰色位置button从状态栏中消失。 一切都很好,直到现在。

当用户使用快速应用程序切换器完成应用程序时,会出现问题,因为之后灰色位置箭头仍显示在状态栏中,尽pipe应用程序未运行。 当用户从他的iPhone中删除应用程序时,灰色位置箭头首先消失。

所以问题是:

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

  • 应用程序需要在后台执行,所以当应用程序进入后台时,无法停止LocationManager。 applicationWillTerminate:当用户使用快速应用程序切换器杀死应用程序时从未被调用。 有什么办法可以收到一个事件,当应用程序被终止使用快速应用程序切换器或终止的操作系统?

  • 我已经看到一些其他的应用程序,当应用程序被快速应用程序切换器杀死时,状态栏中的灰色箭头消失。 这些应用程序如何实现?

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

_ _ _ _ _ _ _ _ _ _ _已 编辑 _ _ _ _ _ _ _ _ _ _ _ _ _
只需将这个varibale添加到你的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"); } 

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

尝试恢复“设置 – >常规 – >重置 – >重置位置警告”中的“位置警告”。 它使用“startupdatinglocation”为我工作。