iOS 每天运行一次代码 [英] iOS Run Code Once a Day

查看:14
本文介绍了iOS 每天运行一次代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个应用背后的想法很简单:下载一个文件.但是,此应用程序适用于并非总是在 Internet 访问范围内的人,因此我需要它知道在上午 9:00 将文件下载到硬盘驱动器.应用程序中也会有一个按钮可以手动执行此操作,但我已经开始使用了.

The idea behind this app is very simple: download a file. However this app will be for people who are not always within internet access range, so I need it to know that at, say 9:00 AM, to download a file to the hard drive. There will be a button within the app to do it manually as well, but I've already got that working.

据我所知,如果可能的话,这将是困难的.我知道 iOS 不喜欢多任务处理,但我也知道它确实允许后台计时器功能.我愿意接受任何人可能必须完成的任何建议,即使这意味着编写一个单独的应用程序.谢谢.

As I understand it, this will be difficult if it is even possible. I know that iOS doesn't like multitasking, but I am also aware that it does allow for background timer functions. I am open to any sort of suggestions anyone might have to accomplish this, even if it means writing a separate app. Thanks.

我看到有可能使用通知,甚至日历.也欢迎该类别中的想法.

I see there is the possibility of working with Notifications, or even maybe the Calendar. Ideas in that category as also welcomed.

编辑 2:我还阅读了一些关于启动应用程序的外部服务器的内容,但没有给出任何描述.

Edit 2: I also read something about an external server initiating an app, but it gave no description.

推荐答案

这里是关于后台执行、通知和计时器等的情况,与应用程序安排某些活动定期发生有关.

Here's the situation regarding background execution and notifications and timers etc. in relation to an app scheduling some activity to happen periodically.

  1. 应用程序不能在后台执行,除非:

  1. An app cannot execute in the background unless:

  1. 它要求操作系统提供额外的时间来这样做.这是使用 beginBackgroundTaskWithExpirationHandler 完成的.Apple 没有(有意地)指定这个额外时间有多长,但实际上大约是 10 分钟.

  1. It requests extra time from the OS to do so. This is done using beginBackgroundTaskWithExpirationHandler. It is not specified (intentionally) by Apple how long this extra time is, however in practice it is around 10 minutes.

一个应用程序有后台模式,模式有:语音、音频、位置、报亭.即使它具有这些类型之一,应用程序也无法在没有一些限制的情况下执行.本讨论的其余部分假设应用没有后台模式.

An app has a background mode, the modes are: voip, audio, location, newstand. Even if it has one of these types an app cannot execute without some restrictions. The rest of this discussion assumes the app does not have a background mode.

  • 当应用程序被暂停时,它无法直接唤醒自己.它之前不能安排 NSTimer,也不能使用诸如 performSelector:afterDelay 之类的东西.等

  • When an app is suspended it cannot do ANYTHING to rouse itself directly. It cannot previously have scheduled an NSTimer, it cannot make use of something like performSelector:afterDelay. etc.

    应用重新激活的唯一方法是用户采取措施使其激活.用户可以通过以下方式执行此操作:

    The ONLY way the app can become active again is if the USER does something to make it active. The user can do this from via of the following:

    1. 直接从其图标启动应用

    1. Launch the app directly from its icon

    启动应用程序以响应应用程序之前在应用程序处于活动状态时安排的本地通知.

    Launch the app in response to a local notification that was previously scheduled by the app while it was active.

    启动应用程序以响应服务器发送的远程通知.

    Launch the app in response to a remote notification sent by a server.

    其他一些:例如,如果应用程序已注册以通过 url 处理启动,则 URL 启动;或者它是否注册为能够处理某种类型的内容.

    A few others: such as URL launching if the app is registered to deal with launching via a url; or if its registered to be capable of dealing with a certain type of content.

  • 如果本地/远程通知触发时应用程序在前台,则应用程序直接接收它.

    If an app is in the foreground when a local/remote notification fires then the app receives it directly.

    如果本地/远程通知触发时应用程序当前不在前台,则应用程序不会收到它.没有在通知触发时执行的代码!

    If the app is not currently in the foreground when a local/remote notification fires then the app DOES NOT receive it. There is no code that is executed when the notification fires!

    只有当用户选择通知时,应用才会激活并可以执行.

    Only IF the user selects the notification will the app become active and it can execute.

    请注意,用户可以针对整个设备或仅针对特定应用程序禁用通知,在这种情况下,用户将永远不会看到它们.如果设备在通知发生火灾时关闭,则通知丢失.

    Note that the user can disable notifications, either for the entire device, or just for a specific application, in which case the user will never see them. If the device is turned off when a notification is due to fire then it is lost.

    这篇关于iOS 每天运行一次代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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