iOS GPS追踪应用程序,一直运行

我试图做一个应用程序来跟踪用户的GPS,这个应用程序是一种汽车GPS跟踪器,一直获取驱动程序的位置,并将其发送到服务器。

我曾尝试将“位置更新”添加到“背景模式”,但应用程序在进入后台10分钟后会自动挂起。

有没有办法让这个应用程序一直运行,并获得GPS位置?

谢谢。

你有两个select:

1)定期跟踪。
这种types的跟踪适用于kCLAuthorizationStatusAuthorizedWhenInUsekCLAuthorizationStatusAuthorizedAlways授权。 当CLLocationManager开始跟踪位置后,它将接收委托方法locationManager:didUpdateLocations:位置更新locationManager:didUpdateLocations: 应用程序可以进入挂起状态,但是当位置pipe理器接收到新位置的应用程序时,会转到后台状态,并在委托方法中处理新的位置。 如何设置位置pipe理器:

 - (void)viewDidLoad { [super viewDidLoad]; self.locationManager = [[CLLocationManager alloc] init]; // Setup location tracker accuracy self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; // Distance filter self.locationManager.distanceFilter = 50.f; // Assign location tracker delegate self.locationManager.delegate = self; // This setup pauses location manager if location wasn't changed [self.locationManager setPausesLocationUpdatesAutomatically:YES]; // For iOS9 we have to call this method if we want to receive location updates in background mode if([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]){ [self.locationManager setAllowsBackgroundLocationUpdates:YES]; } [self.locationManager startUpdatingLocation]; } 

2)指示位置改变跟踪。
这种types的跟踪只适用于kCLAuthorizationStatusAuthorizedAlways授权。 它每500米只接收一个新的位置,所以距离filter和所需的准确度在这里不起作用。 应用程序可以进入挂起状态,甚至可以由系统终止,但是当位置更新应用程序进入后台状态,并在委托方法locationManager:didUpdateLocations:接收到位置时。
如果系统终止了应用程序,则会在didFinishLaunchingWithOptions应用程序委托方法的启动选项中使用UIApplicationLaunchOptionsLocationKey键在后台重新启动。 如何在跟踪中设置此types:

 - (void)viewDidLoad { [super viewDidLoad]; self.locationManager = [[CLLocationManager alloc] init]; // Assign location tracker delegate self.locationManager.delegate = self; // For iOS9 we have to call this method if we want to receive location updates in background mode if([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]){ [self.locationManager setAllowsBackgroundLocationUpdates:YES]; } [self.locationManager startMonitoringSignificantLocationChanges]; } 

您应该注意到这两种方法都不能保证您的应用程序不会进入暂停状态。
此外,如果应用程序被用户终止(例如从应用程序切换器通过滑动)在后台的位置跟踪将无法正常工作。

更新 (对应评论)

这里是我的代码示例,为我工作:
定期跟踪 。 运行示例,提供对用户位置的访问权限,点击“ 开始”button以启动位置更新。 要在模拟器中testing位置,请在仿真器菜单debugging>位置>高速公路驱动器中select。 现在,您可以通过主页button(Command + Shift + H)将应用推送到背景。 离开模拟器超过10分钟,所有这一次的应用程序将收到的位置。 当你回到应用程序,你会看到地图上的红色针脚。
对于重大更改 。 运行应用程序并按照与前面示例相同的方式进行testing。
监视重大更改只能通过方法[self.locationManager startMonitoringSignificantLocationChanges];

更新 (iOS 11)

iOS 11中的位置跟踪更改

iOS 11也对现有的API进行了一些重大的改变。 其中一个受影响的区域是位置跟踪。 如果您的应用仅在应用处于前台时才使用位置 ,与大多数应用一样,您可能不需要更改任何内容; 然而,如果这些应用程序是一天中持续跟踪用户位置的应用程序之一,则应该在今年夏天预定一些时间,对如何跟踪和testing可能的使用情况做一些更改。

请点击此链接: https : //mackuba.eu/2017/07/13/changes-to-location-tracking-in-ios-11/

回答解决scheme中的评论1(我无法在任何地方发表评论):您似乎没有解决问题,因为您的应用程序被暂停,10分钟后不再更新位置。

我有同样的问题:我已经setAllowsBackgroundLocationUpdates设置为YES ,我的Info.plistNSLocationAlwaysUsageDescription键,但我的应用程序也用于停止跟踪10分钟后的位置。

我通过在Info.plist文件中添加NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription来解决它,所以它看起来像这样:

  <key>NSLocationAlwaysUsageDescription</key> <string>This app needs to use your location</string> <key>NSLocationWhenInUseUsageDescription</key> <string>This app needs to use your location</string> 

设置它。它采取电池的权力,但您的应用程序运行在Background.OS不会暂停您的应用程序

[self.locationManager setPausesLocationUpdatesAutomatically:NO];