swift iOS localNotifications [英] swift iOS localNotifications

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

问题描述

我想设置特定时间的本地通知,例如:04:30 Am Everyday,但我无法弄明白。我是iOS开发的新手,所有localNotifications教程都只包括fireDate = NSDate()

I want to set local notifications for specific times, eg : 04:30 Am Everyday, but i cant figure it out. I am new to iOS development and all localNotifications tutorials just cover fireDate = NSDate()

谢谢!

推荐答案

在您想要触发本地通知的函数中尝试此代码:

Try this code in your function you want to fire local notification:

let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let date = NSDate()
let dateComponents = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date)
dateComponents.hour = 4
dateComponents.minute = 30
let notification = UILocalNotification()
notification.alertAction = "Title"
notification.alertBody = "Fire Fire Fire :v"
notification.repeatInterval = NSCalendarUnit.WeekOfYear
notification.fireDate = calendar.dateFromComponents(dateComponents)
UIApplication.sharedApplication().scheduleLocalNotification(notification)

并记得放这个AppDelegate中的代码:

And remember to put this code in your AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {   
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Badge, .Alert], categories: nil))
    return true
  }

这篇关于swift iOS localNotifications的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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