Ios 如何在每5分钟后从后台/前台的应用程序中获取位置

Ios 如何在每5分钟后从后台/前台的应用程序中获取位置,ios,objective-c,cllocationmanager,Ios,Objective C,Cllocationmanager,我怎样才能得到它 -(void)updateLocation { [self performSelector:@selector(updateLocation) withObject:nil afterDelay:300]; [self.locationTracker updateLocationToServer]; } 但它不是每次都能工作。尝试使用NSTimer NSInteger minutes = 5; [NSTimer scheduledTimerWithTimeInterval

我怎样才能得到它

-(void)updateLocation {

[self performSelector:@selector(updateLocation) withObject:nil afterDelay:300];

[self.locationTracker updateLocationToServer];
}
但它不是每次都能工作。

尝试使用NSTimer

NSInteger minutes = 5;
[NSTimer scheduledTimerWithTimeInterval:60 * minutes
                                 target:self
                               selector:@selector(updateLocation)
                               userInfo:nil
                                repeats:YES];

对于要在后台更新的位置,您需要从项目的功能中打开后台模式。

按照NA000022的建议,在功能中添加后台位置更新权限,并将其添加到位置管理器中

self.locationManager.pausesLocationUpdatesAutomatically = NO;
我还建议使用

locationManager.distanceFilter 
locationManager.desiredAccuracy

而不是更新它的持续时间。由于位置更新比任何其他服务消耗电池的速度都快。

那么您面临的确切问题是什么呢。请您再解释一下。我想每隔5分钟获取一次位置,但有时我会在tiime上获取性能位置,但有时我无法获取位置。您在updateLocation函数中做什么?只是好奇..@iphone它的循环函数每5分钟调用一次并获取更新的位置使用计时器或PerformSelect获取GPS位置是错误的,1。您不需要这样做,
CLLocationManager
能够根据您在后台设置的设置/过滤器提供GPS位置。使用计时器,您必须每次启动GPS并尝试获取位置,其缺点是您无法获得准确位置,因为GPS需要几秒钟才能正确定位。最好只使用
CLLocationManager
@iphone:我使用了CLLocationManager«是的,我知道,没有它你就无法获得GPS值,我的观点是你不需要计时器来获取位置,
CLLocationManager
已经为你做了。我已经做了,locationManager.desiredAccuracy=kAllocationAccuracybestforNavigation;locationManager.distanceFilter=KCLDistanceFilterOne;顶线呢?你添加了那个代码吗?我最近做了一件类似的事情,它在后台/前台运行良好。@Bhadreshssonani当应用程序被终止/终止时,您无法获取位置。