带有“位置"的 iOS 长时间运行的后台计时器背景模式 [英] iOS long-running background timer with "location" background mode

查看:21
本文介绍了带有“位置"的 iOS 长时间运行的后台计时器背景模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现长时间运行的后台计时器,它会定期将用户的位置发送到服务器.

I want to implement long-running background timer which sends user's location to server periodically.

为此,我为 UIBackgroundModes 设置了 location 模式,并在应用程序进入后台时调用 beginBackgroundTaskWithExpirationHandler.而对于 CLLocationManager,我使用 startUpdatingLocation 方法(不想使用重大更改,因为我需要高精度定位).

To do this, I set location mode for UIBackgroundModes, and call beginBackgroundTaskWithExpirationHandler when the application goes to background. And for CLLocationManager, I use startUpdatingLocation method (don't want to use significant change, because I need high precision location).

但在应用程序进入后台大约 26 分钟后,它会停止向服务器发送位置信息.但是应用程序并没有崩溃,所以在我将应用程序带到前台后,它可以恢复它的计时器.

But around 26minutes after the app goes to background, it stops to send location to the server. But the app isn't crashed, so after I bring the app to foreground, it can resume its timer.

26 分钟来自实验,使用 iOS 6.1、iPhone 5.

26minutes is from experiments, with iOS 6.1, iPhone 5.

这里有一些问题,

  1. 我不明白为什么它会在 26 分钟而不是 10 分钟后挂起,这被称为后台任务的时间限制.

  1. I can't understand why it suspends after 26mins not 10mins, which is known as time limit for background task.

16 分钟前,backgroundTimeRemaining 方法返回双倍最大值.但是 16 分钟后,它从 600 减少,所以它会在 10 分钟后暂停后台任务.

Before 16mins, backgroundTimeRemaining methods returns double max value. But after 16mins, it decreases from 600, so it suspends the background task after 10mins.

我已经尝试在过期处理程序中调用 beginBackgroundTaskWithExpirationHandler,但没有用.

I already tried to call beginBackgroundTaskWithExpirationHandler inside of expiration handler, but no use.

如果有人知道这个问题的线索,将不胜感激.

If there's anyone who has a clue for this problem, will be greatly appreciated.

推荐答案

https://github.com/voyage11/Location

使用此代码.您可以设置要调用它的时间间隔.我尝试了很多代码,但我发现这是最准确且电池电量最少的问题.这对于后台定位服务也很棒.:)

Use this code.you can set time interval you want to call it. I have tried many codes, but i found this as most accurate and least battery issue.This is also awesome for background location service. :)

这篇关于带有“位置"的 iOS 长时间运行的后台计时器背景模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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