暂停后iPhone背景中的iPhone GPS永远不会恢复

时间:2013-07-05 08:25:35

标签: ios objective-c core-location cllocationmanager uiapplicationdelegate

我的应用程序需要在后台跟踪用户位置更改,并且只要用户四处移动就可以正常工作。 用户停止并在10-20分钟左右后暂停CLLocationManager。该通知表明了这一点:

-(void)locationManagerDidPauseLocationUpdates:(CLLocationManager *)manager{}

这对我也没关系。太棒了,我省了一些电池等等。

问题是CLLocationManager永远不会在用户再次开始移动时醒来,并且在我将应用程序置于前台之后委托方法永远不会被触发(变为活动状态):

//Never called back after CLLocationManager pauses:
-(void)locationManagerDidResumeLocationUpdates:(CLLocationManager *)manager{}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{}

为什么在设备重新开始移动后永远不会调用locationManagerDidResumeLocationUpdates? GPS也不应该自动恢复(因为自动暂停)? 有没有办法在没有用户互动的情况下恢复GPS?

应用程序在Info.plist文件中声明了以下内容:

enter image description here

我的CLLocationManager设置为:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setActivityType:CLActivityTypeFitness];
//I WANT pauses to save some battery, etc... That is why following line is commented out (default)
 //[locationManager setPausesLocationUpdatesAutomatically:NO];
 locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters;
 locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
 [locationManager startUpdatingLocation];

3 个答案:

答案 0 :(得分:30)

如果您在Apple论坛上搜索pausesLocationUpdatesAutomatically,您会发现一个类似行的最近一个问题,该问题得到了Apple开发人员的回复。我不会直接在这里重新发布它,因为它是一个私人论坛,但要点是,当用户忘记他们已经有位置感知应用程序正在运行且已停止使用时,该位置暂停它。通过暂停更新,他们的电池不会快速耗尽。不幸的是,不可能知道新的移动是他们恢复活动还是做其他事情,所以在应用程序回到前台之前不会恢复更新。他们的建议是捕捉暂停调用并以某种方式引起用户的注意,以便他们在需要更新时打开应用程序。

修改

来自Apple自己的pausesLocationUpdatesAutomatically文档。他们建议:

  

暂停发生后,当您确定需要重新启动位置服务时, 负责再次重新启动位置服务。核心位置调用locationManagerDidPauseLocationUpdates(_:)   您的位置经理代表的方法让您知道a   暂停已经发生。在该方法中,您可以配置本地   触发器类型为UNLocationNotificationTrigger的通知   并设置为在用户退出当前区域时通知。该   本地通知的消息应提示用户启动   您的应用再次使其可以恢复更新。

答案 1 :(得分:3)

苹果医生在这个话题上非常薄弱。当苹果认为用户不需要GPS时,属性pausesLocationUpdatesAutomatically允许苹果关闭GPS。

虽然没有记录,但似乎设置此属性会导致在后台模式下停止GPS。

各种帖子描述了该财产的问题:例如: iOS 6 CoreLocation does not work

iOS 6 AutoPause doesn't work  Stanislav Dvoychenko发布了Apple的推荐:

  

[更新 - 2013年3月4日]。我查看了Apple的演示文稿   iOS6中的位置更改,他们建议使用区域更改   一旦获得区域更改事件,监视“取消暂停”。虽然   这不适合我的场景,因为用户可能会去/运行/驾驶   一两公里,直到发生这样的事件。

我建议:将该属性设置为false。

答案 2 :(得分:1)

如果可以,请使用startMonitoringSignificantLocationChanges,因为那时(Apple):

  

如果您启动此服务并且您的应用程序随后终止,则系统会在新事件到达时自动将应用程序重新启动到后台。

一旦您的应用程序唤醒,您可以尝试将其与更细粒度的跟踪相结合(例如,用户重新输入)。