swift iOS localNotifications

编程入门 行业动态 更新时间:2024-10-26 10:36:25
本文介绍了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!ponents([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

本文发布于:2023-08-05 17:04:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1306842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:swift   iOS   localNotifications

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!