每天在 Swift 中的固定时间通知? [英] Notification in Swift every day at a set time?

查看:89
本文介绍了每天在 Swift 中的固定时间通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人感到困惑并认为这是我昨天提出的问题的重复,那不是.我在那里问如何每天调用一个函数,在这里我问如何在每天的特定时间调用通知.

我正在寻找一种每天早上 7 点重复本地通知的方法.我目前有这个代码设置来获取日、月、年等.

I am looking for a way to repeat a local notification every day at 7.00AM. I currently have this code setup to get the day, month, year etc.

let date = NSDate()
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay, fromDate: date)
    let hour = components.hour
    let minutes = components.minute
    let month = components.month
    let year = components.year
    let day = components.day

如何在每天早上 7 点时调用通知?

How do I call a notification every day when the time is 7.00 AM?

推荐答案

首先像你一样创建日历对象:

First create the calendar object as you did:

var calendar = NSCalendar()
var calendarComponents = NSDateComponents()
calendarComponents.setHour(7)
calendarComponents.setSeconds(0)
calendarcomponents.setMinutes(0)
calendar.setTimeZone(NSTimeZone.defaultTimeZone)
var dateToFire = calendar.dateFromComponents(calendarComponents)

现在我们可以每天安排通知.

Now we can schedule the notification daily.

localNotification.fireDate = dateToFire
localNotification.setTimeZone(NSTimeZone.defaultTimeZone)
localNotification.setRepeatInterval(kcfCalendarUnitDay)

语法可能并不完美,我是从 Obj-C 翻译过来的,但您应该了解总体思路.

Syntax might not be perfect, I was translating from Obj-C, but you should get the general idea.

这篇关于每天在 Swift 中的固定时间通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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