gpt4 book ai didi

ios - 快速 iOS 本地通知

转载 作者:行者123 更新时间:2023-11-29 01:07:59 25 4
gpt4 key购买 nike

我想为特定时间设置本地通知,例如:每天凌晨 04:30,但我无法弄清楚。我是 iOS 开发的新手,所有 localNotifications 教程都只涵盖 fireDate = NSDate()

谢谢!

最佳答案

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

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 中:

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

关于ios - 快速 iOS 本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36126988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com