iOS-如何每天安排一些时间? [英] iOS - How can I schedule something once a day?

查看:127
本文介绍了iOS-如何每天安排一些时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有NSTimer.scheduledTimerWithInterval

这样使用:

override func viewDidLoad() {
    super.viewDidLoad()

    var timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}

func update() {
    // Something cool
}

但是我认为var timer所在的视图也必须存在,即:无法关闭该视图(对吗?)

如何每天在iOS中安排一些时间,例如:每天晚上8点发送通知?

Android中,我通过以下方式实现了此目的:

  1. 我使用了一个叫做AlarmManager的东西,它与iOS scheduledTimerWithInterval类似,但是间隔很长时间 ,它在后台服务中运行.

  2. 在启动(引导)时,还有另一个后台服务会再次设置警报.这涵盖了Android设备关闭(因此后台服务也关闭)的情况.

那么,在iOS中,是否有类似scheduledTimerWithInterval这样的大间隔时间?

如果重新启动iPhone/iPad,我是否需要重新设置间隔?

解决方案

是的,要使用NSTimer,该应用必须在前台或后台运行.但是苹果公​​司非常特别,它只允许某些类型的应用程序在后台继续运行(以确保我们不会让应用程序随机以自己的特权运行,并在此过程中耗尽电池电量和/或影响我们的性能)在使用设备时).

  1. 当您说"notification"时,您的意思是真的要通知用户吗?

    在这种情况下,这里的替代方法是创建UILocalNotification,这是用户通知(假设他们已授予您的应用执行通知的权限),即使您的应用未运行,该通知也会显示. >

    例如,要注册本地通知:

    let application = UIApplication.sharedApplication()
    let notificationTypes: UIUserNotificationType = .Badge | .Sound | .Alert
    let notificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
    application.registerUserNotificationSettings(notificationSettings)
    

    然后安排重复通知:

    let notification = UILocalNotification()
    notification.fireDate = ...
    notification.alertTitle = ...
    notification.alertBody = ...
    notification.repeatInterval = .CalendarUnitDay
    application.scheduleLocalNotification(notification)
    

    有关更多信息,请参见本地和远程通知编程指南.

  2. 或者您的意思是启动一些过程,例如从远程服务器获取数据.

    如果您希望应用程序即使未运行也要获取数据,则可以使用后台获取.参见机会性地获取少量内容.

    请注意,使用后台获取,您无需指定何时检索数据,而是由系统自行选择是否检查数据.据报道,它受以下因素的影响:用户使用该应用程序的频率,请求查看数据是否存在的频率导致实际上存在要检索的新数据等.您无法直接控制这些后台获取的时间./p>

I know there is NSTimer.scheduledTimerWithInterval

Which is used like this:

override func viewDidLoad() {
    super.viewDidLoad()

    var timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}

func update() {
    // Something cool
}

But I assume that the view where var timer is living must be living too, ie: can't close that view (am I right?)

How can I schedule something once a day in iOS, eg: everyday at 8pm send a notification?

In Android I've achieved this the following way:

  1. I've used a thing called AlarmManager, that is similar to the iOS scheduledTimerWithInterval but for large intervals, which is running inside a background service.

  2. At the startup (boot), there is another background service that setups the alarm again. This covers the case when the Android device is shut down (and so the background service is shut down too)

So, in iOS, is there something like scheduledTimerWithInterval for large intervals?

Will I need to set again the interval if the iPhone/iPad is rebooted?

解决方案

Yes, to use NSTimer, the app has to be running either in foreground or background. But Apple is quite particular only allowing certain types of apps to continue to run in the background (in an effort to make sure we don't have apps randomly running on their own prerogative and killing our batteries in the process and/or affecting our performance while using the device).

  1. When you say "notification", do you really mean notifying the user of something?

    In that case, the alternative here is to create a UILocalNotification, which is a user notification (assuming they've granted your app permission to perform notifications), which is presented even when your app is not running.

    For example, to register for local notifications:

    let application = UIApplication.sharedApplication()
    let notificationTypes: UIUserNotificationType = .Badge | .Sound | .Alert
    let notificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
    application.registerUserNotificationSettings(notificationSettings)
    

    And then to schedule the repeating notification:

    let notification = UILocalNotification()
    notification.fireDate = ...
    notification.alertTitle = ...
    notification.alertBody = ...
    notification.repeatInterval = .CalendarUnitDay
    application.scheduleLocalNotification(notification)
    

    For more information, see the Local and Remote Notification Programming Guide.

  2. Or do you mean initiating some process, such as fetching data from a remote server.

    If you want the app to fetch data even if your app isn't running, you can use background fetch. See Fetching Small Amounts of Content Opportunistically in the App Programming Guide for iOS.

    Note, with background fetch, you don't specify when data is to be retrieved, but rather the system will check for data at a time of its own choosing. It reportedly factors in considerations ranging from how often the user uses the app, how often requests to see if there is data result in there actually being new data to retrieve, etc. You have no direct control over the timing of these background fetches.

这篇关于iOS-如何每天安排一些时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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