一直运行的 iOS GPS 跟踪应用程序

标签 ios gps core-location

我正在尝试制作一个始终跟踪用户 GPS 的应用程序,该应用程序是一种车载 GPS 跟踪器,可以始终获取驾驶员的位置并将其发送到服务器。

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

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

最佳答案

这里有两个选择:

1) 常规位置跟踪。
这种类型的跟踪适用于 kCLAuthorizationStatusAuthorizedWhenInUsekCLAuthorizationStatusAuthorizedAlways 授权。当 CLLocationManager 开始跟踪位置时,它将在委托(delegate)方法 locationManager:didUpdateLocations: 中接收位置更新。应用程序可以进入暂停状态,但是当位置管理器接收到新位置时,应用程序进入后台状态并在委托(delegate)方法中处理新位置。如何设置位置管理器:

- (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) Signification location changes tracking.
这种类型的跟踪仅适用于 kCLAuthorizationStatusAuthorizedAlways 授权。它每 500 米接收一次新位置,因此距离过滤器和 desiredAccuracy 在这里不起作用。应用可以进入暂停状态,甚至可以被系统终止,但是当位置更新时应用进入后台状态并在委托(delegate)方法 locationManager:didUpdateLocations: 中接收位置。
如果应用被终止通过系统,它将在后台重新启动,在 didFinishLaunchingWithOptions 应用程序委托(delegate)方法中使用 UIApplicationLaunchOptionsLocationKey 键启动选项。如何设置这种类型的跟踪:

- (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];
}


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


UPDATE(对应评论)

以下是适合我的代码示例:
For Regular tracking .运行示例,提供对用户位置的访问权限,点击开始按钮开始位置更新。要在模拟器中测试位置,请在模拟器菜单 Debug > Location > Freeway Drive 中选择。现在您可以通过主页按钮 (Command+Shift+H) 将应用程序推送到后台。离开模拟器 10 分钟以上,此时应用程序将一直接收位置信息。当您返回应用程序时,您会在 map 上看到红色图钉。
For Significant changes .运行应用程序并按照与上一个示例相同的方式进行测试。
监控重大变化只能通过方法[self.locationManager startMonitoringSignificantLocationChanges];

开始

更新(iOS 11)

iOS 11 中位置跟踪的变化

iOS 11 还对现有 API 进行了一些重大更改。受影响的领域之一是位置跟踪。 如果您的应用仅在应用处于前台时使用位置信息,就像大多数应用一样,您可能根本不需要更改任何内容;但是,如果它是那些全天持续跟踪用户位置的应用程序之一,您可能应该在今年夏天预订一些时间,对您进行跟踪和测试可能的使用场景的方式进行一些更改。

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

关于一直运行的 iOS GPS 跟踪应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35515528/

相关文章:

javascript - 如何在 Node.js 中使用 GPS?

ios - 如何远程跟踪 iOS 用户位置?

ios - 解析没有键的Json数组swift 4

ios - 在 Swift 框架中打包 XIB 进行分发?

ios - iOS 中流畅的视频循环

c# - 与 TomTom 设备连接(在计算机上显示设备屏幕)

ios - 在用 Swift 编写的 iOS 应用程序中实现 Shopify

java - 使用纬度/经度计算从点 A 到线段的距离

ios - 显着区域变化/区域监测

swift - 提供获取当前速度的简单方法(实现速度计)