每天在设定的时间与swift重复本地通知 [英] Repeating local notification daily at a set time with swift

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

问题描述

我是iOS开发的新手,但是已经创建了应用程序,我试图在设定的时间内创建每日通知。目前,通知对于给定的日期/时间执行一次。我不确定如何使用repeatInterval方法来每天安排。每天重复通知的最佳方法是什么?任何帮助将非常感谢(Y)。

  var dateComp:NSDateComponents = NSDateComponents()
dateComp.year = 2015;
dateComp.month = 06;
dateComp.day = 03;
dateComp.hour = 12;
dateComp.minute = 55;
dateComp.timeZone = NSTimeZone.systemTimeZone()

var calender:NSCalendar = NSCalendar(calendarIdentifier:NSCalendarIdentifierGregorian)!
var date:NSDate = calender.dateFromComponents(dateComp)!

var notification:UILocalNotification = UILocalNotification()
notification.category =Daily Quote
notification.alertBody = quoteBook.randomQuote()
notification.fireDate = date
notification.repeatInterval =

UIApplication.sharedApplication()。scheduleLocalNotification(notification)



。您必须提供NSCalendarUnit值,例如HourCalendarUnit或DayCalendarUnit才能重复通知。

只需添加此代码即可每天重复本地通知:

  notification.repeatInterval = NSCalendarUnit.CalendarUnitDay 


I am new to iOS development, but have created the app and I am trying to create a daily notification for a set time. Currently the notification executes once for the given date/time. I am unsure how to use the repeatInterval method to schedule it daily. What is the best method to repeat the notification daily ? any help would be much appreciated (Y).

    var dateComp:NSDateComponents = NSDateComponents()
    dateComp.year = 2015;
    dateComp.month = 06;
    dateComp.day = 03;
    dateComp.hour = 12;
    dateComp.minute = 55;
    dateComp.timeZone = NSTimeZone.systemTimeZone()

    var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    var date:NSDate = calender.dateFromComponents(dateComp)!

    var notification:UILocalNotification = UILocalNotification()
    notification.category = "Daily Quote"
    notification.alertBody = quoteBook.randomQuote()
    notification.fireDate = date
    notification.repeatInterval = 

    UIApplication.sharedApplication().scheduleLocalNotification(notification)

解决方案

You have to provide an NSCalendarUnit value like "HourCalendarUnit" or "DayCalendarUnit" for repeating a notification.

Just add this code to repeat the local notification daily :

notification.repeatInterval = NSCalendarUnit.CalendarUnitDay

这篇关于每天在设定的时间与swift重复本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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