后台的iOS计时器 [英] iOS Timer in the background

查看:83
本文介绍了后台的iOS计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的iOS应用程序在后台运行且关闭时启动计时器.计时器必须每30分钟检查一次新的通知.在计时器函数中,他们每隔30分钟调用另一个函数showNotification().

I want in my iOS application to start a timer when the app is running in the background and when the app is closed. The timer must check every 30 minutes of there a new notifications. In the timer function they call every 30 minutes another function showNotification().

如何执行此计时器以及当应用程序不在后台运行/在后台运行时我必须在哪个位置调用计时器.

How do I do this timer and on which place must I call the timer when the app is not running/run in the background.

推荐答案

当应用程序不在前台且具有100%的确定性时,将无法执行任何操作.您可以使用后台提取来定期唤醒,但是您无法控制它何时发生.

It is impossible to do something when the application is not in the foreground, with 100% certainty. You can use background fetch to be woken up periodically, but you can not control when it happens.

虽然存在一些技术上的变通办法,甚至可能还有一些骇人听闻的解决方案(在后台播放无声音频),但在我看来,无需使用计时器和后台抓取就可以解决您的问题.

While there are some technical workarounds, and potentially even some hacky solutions (playing silent audio in the background), it sounds to me like your problem can be solved without the use of timers and background fetch.

只需实施远程通知.当您的iOS收到有关您的应用程序的通知时,它将唤醒应用程序并等待一段时间.然后,您可以控制向用户显示哪些通知,甚至可以在后台加载一些数据.

Simply implement remote notifications. When your iOS receives a notification for your app, it will wake the app up and let it process for a while. You can then control what notifications are shown to the user, and maybe load some data in the background.

广义上,您需要:

  • 在应用启动时注册远程通知(通常在application:didFinishLaunching:中)
  • 当您收到通知令牌时,将其发送到将向您发送通知的Web服务器(此令牌可以更改,因此请确保在启动应用程序时始终将其发送到Web服务器)
  • 当有新内容可用时,您的Web服务器可以使用令牌将有效负载发送到您的应用程序(您选择的语言可能已经具有用于此目的的库.例如,如果您使用Ruby,则有 查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆