TMM*_*Dev 20 gps core-location ios
我正在尝试制作一个应用程序来跟踪用户GPS,这个应用程序是一种车载GPS跟踪器,以获取驱动程序的位置,并将其发送到服务器.
我试图将"位置更新"添加到"后台模式",但应用程序将在进入后台10分钟后自动暂停.
有没有办法让这个应用程序一直运行并获得GPS位置?
谢谢.
shp*_*sta 16
这里有两个选项:
1)定期跟踪位置.
此类跟踪适用于kCLAuthorizationStatusAuthorizedWhenInUse和kCLAuthorizationStatusAuthorizedAlways授权.当CLLocationManager开始跟踪的位置,一旦将委托方法接收位置更新locationManager:didUpdateLocations:.应用程序可以进入暂停状态,但是当位置管理器收到新位置应用程序进入后台状态并在委托方法中处理新位置时.如何设置位置管理器:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
2)指示位置变化跟踪.
此类跟踪仅适用于kCLAuthorizationStatusAuthorizedAlways授权.它只接收每500米的新位置,因此距离过滤器和所需的精度在这里不起作用.应用程序可以进入暂停状态,甚至可以被系统终止,但是当位置更新应用程序进入后台状态并在委托方法中接收位置时locationManager:didUpdateLocations:.
如果应用程序被系统终止,它将在后台重新启动,并UIApplicationLaunchOptionsLocationKey在didFinishLaunchingWithOptionsapp delegate方法中使用启动选项中的键.如何在跟踪时设置此类型:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
您应该注意到这两种方法都不能保证您的应用程序不会进入挂起状态.
此外,如果应用程序被用户终止(例如,通过轻扫来自应用程序切换器),则后台中的位置跟踪将不起作用.
更新(对应于注释)
这是我的代码示例,适合我:
对于常规跟踪.运行示例,提供对用户位置的访问权限,点击" 开始"按钮以启动位置更新.要在模拟器中测试位置,请在模拟器菜单Debug> Location> Freeway Drive中选择.现在您可以通过主页按钮(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/
我相信它对作者有用,因为这个问题是在 2016 年 2 月提出的,我在 2019 年 6 月给出了答案。这个答案可能对其他用户有用。
最近,我正在处理相同的要求。经过 2-3 周的努力,我做到了。对于其他用户,我为它创建了一个帮助类。可在 GitHub 中找到。
请根据您的要求使用HSLocationManager。我在我的一个项目中达到了相同的要求
位置管理器,允许每 n 秒以所需的位置精度获取后台位置更新。
优势:
如果位置管理器当前正在运行,操作系统永远不会终止我们的应用程序。
在需要时定期进行位置更新(范围在 2 - 170 秒之间(受最大允许后台任务时间限制))
可定制的定位精度和时间段。
低内存消耗(单例类)
| 归档时间: |
|
| 查看次数: |
12836 次 |
| 最近记录: |