一直运行的 iOS GPS 跟踪应用程序 [英] iOS GPS tracking app that runs all the time

查看:30
本文介绍了一直运行的 iOS GPS 跟踪应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make an app to track the user GPS all the time, this app is a kind of car GPS tracker to get the location of driver all the time and send it to server.

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

I have tried to add "location updates" to the "background modes" but the app will automatically suspends after 10 mins when going into background.

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

Is there a way to make this app run all the time and get the GPS location?

推荐答案

这里有两种选择:

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

You have two options here:

1) Regular location tracking.
This type of tracking works with kCLAuthorizationStatusAuthorizedWhenInUse and kCLAuthorizationStatusAuthorizedAlways authorizations. When CLLocationManager started tracking location once it will receive location updates in delegate method locationManager:didUpdateLocations:. App can go to suspended state, but when location manager receive new location app goes to background state and handles new location in delegate method. How to setup location manager:

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


2) Signification location changes tracking.
This type of tracking works only with kCLAuthorizationStatusAuthorizedAlways authorization. It receives new location only each 500 meters, so distance filter and desiredAccuracy don't work here. App can go to suspended state, and even can be terminated by system, but when location updates app goes to background state and receives location in delegate method locationManager:didUpdateLocations:.
If app was terminated by system, it will be relaunched in background with UIApplicationLaunchOptionsLocationKey key in launch options in didFinishLaunchingWithOptions app delegate method. How to setup this type on tracking:

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


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


You should notice that both of these methods does not guarantee that your application does not go to suspended state.
Also, if app was terminated by user (for example from app switcher by swipe) location tracking in background will not work.


UPDATE(对应评论)

这是对我有用的代码示例:
常规跟踪.运行示例,提供对用户位置的访问,点击 Start 按钮开始位置更新.要在模拟器中测试位置,请在模拟器菜单 Debug > Location > Freeway Drive 中选择.现在您可以通过主页按钮 (Command+Shift+H) 将应用程序推到后台.离开模拟器超过 10 分钟,所有这段时间应用程序都会收到位置.当您返回应用程序时,您会在地图上看到红色图钉.
重大更改.以与上一个示例相同的方式运行应用程序并进行测试.
监控重大变化只能通过[self.locationManager startMonitoringSignificantLocationChanges];


UPDATE (corresponding to comments)

Here is my code examples that work for me:
For Regular tracking. Run the example, provide access to user location, tap Start button to start location updates. To test locations in simulator choose in simulator menu Debug > Location > Freeway Drive. Now you can push app to background by home button (Command+Shift+H). Leave simulator for more than 10 minutes, and all this time app will receive locations. When you return to app you will see red pins on the map.
For Significant changes. Run the app and test by the same way as for previous example.
Monitoring Significant changes can be started only by method [self.locationManager startMonitoringSignificantLocationChanges];

更新(iOS 11)

iOS 11 中位置跟踪的变化

Changes to location tracking in iOS 11

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

iOS 11 also makes some major changes to existing APIs. One of the affected areas is location tracking. If your app only uses location while the app is in the foreground, as most apps do, you might not have to change anything at all; however, if it’s one of those apps that continuously track user’s location throughout the day, you should probably book some time this summer for making some changes in how you do the tracking and testing possible usage scenarios.

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

这篇关于一直运行的 iOS GPS 跟踪应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆