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

时间:2016-03-25 19:46:56

标签: ios swift notifications

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

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 {


            }

        }

    }
}

1 个答案:

答案 0 :(得分:0)

您可以在安排时将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,但它是一个词典