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

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

问题描述

我试图让应用程序始终跟踪用户的GPS,这个应用程序是一种汽车GPS跟踪器,可以随时获取驱动程序的位置并将其发送到服务器。



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



谢谢。

解决方案

您有两种选择:


1)定期跟踪定位。
此类跟踪工作使用 kCLAuthorizationStatusAuthorizedWhenInUse kCLAuthorizationStatusAuthorizedAlways 授权。当 CLLocationManager 开始跟踪位置,它将在委托方法 locationManager:didUpdateLocations:中接收位置更新。应用程序可以进入挂起状态,但是当位置管理器接收到新的位置应用程序时,它将转到后台状态并在委托方法中处理新位置。如何设置位置管理器:

   - (void)viewDidLoad {
[super viewDidLoad];

self.locationManager = [[CLLocationManager alloc] init];

//设置位置跟踪器的准确性
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;

//距离过滤器
self.locationManager.distanceFilter = 50.f;

//分配位置跟踪器委托
self.locationManager.delegate = self;

//如果位置没有改变,这个设置会暂停位置管理器
[self.locationManager setPausesLocationUpdatesAutomatically:YES];

//对于iOS9我们必须调用这个方法,如果我们想在后台模式下接收位置更新
if([self.locationManagerrespondsToSelector:@selector(allowBackgroundLocationUpdates)]){
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
}

[self.locationManager startUpdatingLocation];
}


2)指示位置变更追踪
此类追踪仅适用于 kCLAuthorizationStatusAuthorizedAlways 授权。它每500米只接收一个新的位置,所以距离过滤器和期望的精度在这里不起作用。应用程序可以进入挂起状态,甚至可以由系统终止,但是当位置更新应用程序进入后台状态并在委托方法 locationManager:didUpdateLocations:中接收到位置时。 >如果应用程序被系统终止,它将在 didFinishLaunchingWithOptions 应用程序委托中的启动选项中以 UIApplicationLaunchOptionsLocationKey 键的后台重新启动。方法。如何设置这种类型的跟踪:

   - (void)viewDidLoad {
[super viewDidLoad];

self.locationManager = [[CLLocationManager alloc] init];

//分配位置跟踪器委托
self.locationManager.delegate = self;

//对于iOS9我们必须调用这个方法,如果我们想在后台模式下接收位置更新
if([self.locationManagerrespondsToSelector:@selector(allowBackgroundLocationUpdates)]){
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
}

[self.locationManager startMonitoringSignificantLocationChanges];

你应该注意到这两种方法并不能保证你的应用程序不会进入挂起状态。

另外,如果应用程序被用户终止(例如通过刷卡方式从应用程序切换器中),则后台中的位置跟踪将无法使用。




UPDATE (对应于评论)


这里是我的代码示例,适用于我:

用于定期跟踪。运行示例,提供对用户位置的访问权限,点击开始按钮开始位置更新。要在模拟器中测试位置,请在模拟器菜单调试>位置>高速公路驱动器中进行选择。现在,您可以通过主页按钮将应用推送到背景(Command + Shift + H)。离开模拟器超过10分钟,所有这段时间的应用程序将收到位置。当你回到应用程序时,你会在地图上看到红色的针脚。

对于重大更改。运行应用程序并按照与前面示例相同的方式进行测试。

监视重大更改只能通过方法 [self.locationManager startMonitoringSignificantLocationChanges];



更新(iOS 11)

更改iOS 11中的位置跟踪



iOS 11还对现有API进行了一些重大更改。其中一个受影响的区域是位置跟踪。 如果您的应用仅在应用处于前台时使用位置,与大多数应用一样,您可能不需要更改任何内容;然而,如果它是一天中持续跟踪用户位置的应用程序之一,那么您应该在今年夏天预定一些时间,以便对跟踪和测试可能的使用场景进行一些更改。



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


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.

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

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

Thank you.

解决方案

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

UPDATE (iOS 11)

Changes to location tracking in iOS 11

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.

follow this link: https://mackuba.eu/2017/07/13/changes-to-location-tracking-in-ios-11/

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

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