gpt4 book ai didi

ios - 使用 UIDatePicker 在特定时间停止通知

转载 作者:行者123 更新时间:2023-11-30 13:34:34 25 4
gpt4 key购买 nike

我设法使用 UIDatePicker 设置通知,但我的问题是我想通过使用其他 UIDatePicker (datePickerStop) 来停止它们。所以基本上它安排在 Switch 对象和 if 语句中使用两个不同的 UIDatePicker 来启动和结束通知。这是我拥有的代码,但它似乎不起作用,或者我没有将其放在正确的位置。有什么想法吗?

let notificationArray = UIApplication.sharedApplication().scheduledLocalNotifications!

for notification in notificationArray {
if notification.fireDate == fixedNotificationDate(datePickerStop.date) {
UIApplication.sharedApplication().cancelLocalNotification(notification)
}
}

这是完整的代码

override func viewDidLoad() {
super.viewDidLoad()

updateUI()

let defaults = NSUserDefaults.standardUserDefaults()

if (defaults.objectForKey("SwitchState") != nil) {
SwitchOutlet.on = defaults.boolForKey("SwitchState")
}
if (SwitchOutlet.on) {

Label1Outlet.text = "Notifications are On"

} else {
Label1Outlet.text = "Notifications are Off"

}

// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func Switch(sender: AnyObject) {
let defaults = NSUserDefaults.standardUserDefaults()

if (SwitchOutlet.on) {

Label1Outlet.text = "Notifications are On"


defaults.setBool(true, forKey: "SwitchState")
let notification = UILocalNotification()
notification.fireDate = fixedNotificationDate(datePicker.date)
notification.repeatInterval = NSCalendarUnit.Minute
notification.alertBody = “Message”
notification.alertAction = “Lock!”
notification.soundName = "NotifSound.caf"
notification.userInfo = ["CustomField1": "w00t"]
guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() else { return }

if settings.types == .None {
let ac = UIAlertController(title: "Can't Activate", message: "Either we don't have permission to schedule notifications, or we haven't asked yet.", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
return
}


UIApplication.sharedApplication().scheduleLocalNotification(notification)



} else {

Label1Outlet.text = "Notifications are Off"
defaults.setBool(false, forKey: "SwitchState")
UIApplication.sharedApplication().cancelAllLocalNotifications()


}
}



func fixedNotificationDate(dateToFix: NSDate) -> NSDate {

let dateComponents: NSDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Day, NSCalendarUnit.Month, NSCalendarUnit.Year, NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: dateToFix)

dateComponents.second = 0

let fixedDate: NSDate = NSCalendar.currentCalendar().dateFromComponents(dateComponents)!

return fixedDate


}

func updateUI() {

let currentSettings = UIApplication.sharedApplication().currentUserNotificationSettings()

if currentSettings?.types != nil {

if currentSettings!.types == [UIUserNotificationType.Badge, UIUserNotificationType.Alert] {

}
else if currentSettings!.types == [UIUserNotificationType.Badge] {


}
else if currentSettings!.types == UIUserNotificationType.None {


}

}

}
}

最佳答案

您可以在安排时将 userInfo 添加到通知中

let notification:UILocalNotification = UILocalNotification()
notification.userInfo = ["day": day, "hour": hour]
UIApplication.sharedApplication().scheduleLocalNotification(notification)

当您尝试取消时,只需通过 userInfo 找到它即可

let app = UIApplication.sharedApplication()
let day: Int = 1
let hour: Int = 2
for singleNotification in app.scheduledLocalNotifications!{
let notDay = singleNotification.userInfo!["day"] as! Int
let notHour = singleNotification.userInfo!["hour"] as! Int
if(notDay == day && notHour == hour){
app.cancelLocalNotification(singleNotification)
break;
}
}

我使用了 Int,但它是一个字典

关于ios - 使用 UIDatePicker 在特定时间停止通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36226938/

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