我正在实现一种跟踪应用程序,我需要发现一次位置并每10到20秒发送一次(期间值很重要,不能超过). 为了降低电池消耗,我停止了位置更新.这在前台效果很好,但是当应用程序在后台移动时我该怎么办? 我查看了有关后台抓取的信息,但没有确切的时间定期发送数据
I am implementing a kind of tracking app and i need to spot my location once and send it once per 10 - 20 seconds (period value is important and can't be exceeded). To lower battery consumption i stop location updates. This works good in foreground, but how can i do it when app moved in background? I looked info about background fetch, but it hasn't got precise time for periodic sending data
我如何执行此任务?
推荐答案您可以在应用程序处于后台时启动和停止定期位置更新. 要从位置更新的给定链接中实现此添加类.
You can start and Stop periodic location update while app is in background. To achieve this add class from given link for Location Update.
之后,在您的AppDelegate中导入LocationTracker.h.
After that import LocationTracker.h in your AppDelegate.
在didFinishLaunchingWithOptions中添加以下代码.
Add Below code in your didFinishLaunchingWithOptions.
let locationTracker : LocationTracker = LocationTracker(); locationTracker?.startLocationTracking();在LocationTracker.m中,您可以设置重新启动更新的持续时间.在这里,我将其设置为1分钟或60秒.
In LocationTracker.m, you can set duration to restart update.Here i set 1 minute or 60 Seconds.
//Restart the locationMaanger after 1 minute self.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(restartLocationUpdates) userInfo:nil repeats:NO];您还可以设置获取位置的持续时间.我在这里获取位置10秒钟.
You can also set duration time for fetch Locations. Here I fetch location for 10 Seconds.
self.shareModel.delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(stopLocationDelayBy10Seconds) userInfo:nil repeats:NO];更多推荐
定期在后台模式下向服务器发送小数据
发布评论