ios - 在后台模式下定期向服务器发送小数据

标签 ios swift background-process multitasking

我正在实现一种跟踪应用程序,我需要定位一次我的位置并每 10 - 20 秒发送一次(周期值很重要,不能超过)。 为了降低电池消耗,我停止位置更新。这在前台效果很好,但是当应用程序移到后台时我该怎么办? 我查看了有关后台获取的信息,但没有得到定期发送数据的精确时间

我如何执行此任务?

最佳答案

您可以在应用程序处于后台时启动和停止定期位置更新。 要从 Location Update 的给定链接实现此添加类.

之后,在您的 AppDelegate 中导入 LocationTracker.h。

在您的 didFinishLaunchingWithOptions 中添加以下代码。

let locationTracker : LocationTracker  = LocationTracker();
locationTracker?.startLocationTracking();

在LocationTracker.m中,可以设置重启更新的时长,这里我设置的是1分钟或者60秒。

//Restart the locationMaanger after 1 minute
self.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self
                                                       selector:@selector(restartLocationUpdates)
                                                       userInfo:nil
                                                        repeats:NO];

您还可以设置获取位置的持续时间。在这里,我获取了 10 秒的位置。

self.shareModel.delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10 target:self
                                                selector:@selector(stopLocationDelayBy10Seconds)
                                                userInfo:nil
                                                 repeats:NO];

关于ios - 在后台模式下定期向服务器发送小数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34512007/

相关文章:

ios - Xcode Swift Playground 中的多个 View

iphone - 编辑后保存/加载文本字段

ios - 如果未显示 View Controller (导航 Controller 的后堆栈),则不会调用 Firestore addSnapshotListener

ios - .onAppear 是否导致文本在 SwiftUI 中消失?

ios - 如何在微调中使用 OpenEars?

objective-c - 如何将基于十进制的时间转换为 NSDate?

PHP 在后台进程完成后启动脚本?

iphone - 使用 IOS SDK 是否可以在另一个调用正在进行时(在后台)检测来电?

iphone - 如何避免iOS应用程序在后台过渡时暂停?

ios - Hook 任何事件并计算时间执行