Swift-在用户选择的时间推送通知
如何使用日期选择器让用户选择时间,然后向用户发送本地通知?以下是一个简单的代码,用于在swift中安排本地通知:Swift-在用户选择的时间推送通知,swift,date,ios8,notifications,picker,Swift,Date,Ios8,Notifications,Picker,如何使用日期选择器让用户选择时间,然后向用户发送本地通知?以下是一个简单的代码,用于在swift中安排本地通知: let calendar: NSCalendar = NSCalendar.currentCalendar() let fireDateOfNotification: NSDate = //The date which was picked from the picker var notification = UILocalNotification() notification.
let calendar: NSCalendar = NSCalendar.currentCalendar()
let fireDateOfNotification: NSDate = //The date which was picked from the picker
var notification = UILocalNotification()
notification.timeZone = NSTimeZone.localTimeZone()
notification.alertBody = "ALERT STRING"
notification.soundName = UILocalNotificationDefaultSoundName
notification.fireDate = fireDateOfNotification
notification.userInfo = ["UUID": "NotificationID"] //id for the local notification in case you want to cancel it
UIApplication.sharedApplication().scheduleLocalNotification(notification)
如果要取消本地通知,应使用以下功能:
func cancelLocalNotificationsWithUUID(uuid: Int) {
for item in UIApplication.sharedApplication().scheduledLocalNotifications {
let notification = item as! UILocalNotification
if let notificationUUID = notification.userInfo?["UUID"] as? Int {
if notificationUUID == uuid {
UIApplication.sharedApplication().cancelLocalNotification(notification)
}
}
}
}
我希望这能有所帮助。这个问题对于堆栈溢出的格式来说太宽了。你是在尝试。这里至少有两个问题。如何让用户使用日期选择器选择日期时间并将其保存为日期对象?如何在特定日期和时间发送本地通知?