Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

ios - Setting Multiple Times for Notifications in Swift

So I am using Swift now and I had one notification appearing at a certain time when one switch was activated.

However I would like another notification to come up at another time when another switch is activated.

Here is my code for ViewController:

 @IBOutlet var myDatePicker: UIDatePicker!

@IBOutlet var mySwitch: UISwitch!

@IBOutlet var mySwitchTwo: UISwitch!


var localNotification:UILocalNotification = UILocalNotification()
var localNotificationTwo:UILocalNotification = UILocalNotification()

func datePicker()            { myDatePicker.datePickerMode = UIDatePickerMode.Date }
func datePickerDefaultDate() { myDatePicker.date = NSDate().xDays(+1) }


func toggleSwitch(){
    if mySwitch.on{
        localNotification.alertAction = "Open App"
        localNotification.alertBody = "Please take your medication."
        localNotification.fireDate = myDatePicker.date.fireDate
        localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay
        localNotification.timeZone = NSTimeZone.localTimeZone()
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    } else {
        localNotification.alertAction = "Open App"
        localNotification.alertBody = "This notification should not appear"
        localNotification.fireDate = NSDate(timeIntervalSinceNow: 999999999999)
    }
}

func toggleSwitchTwo() {
    if mySwitchTwo.on{
        localNotification.alertAction = "Open App"
        localNotification.alertBody = "Please take your medication."
        localNotification.fireDate = myDatePicker.date.fireDateTwo
        localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay
        localNotification.timeZone = NSTimeZone.localTimeZone()
        UIApplication.sharedApplication().scheduleLocalNotification(localNotificationTwo)
    } else {
        localNotification.alertAction = "Open App"
        localNotification.alertBody = "This notification should not appear"
        localNotification.fireDate = NSDate(timeIntervalSinceNow: 999999999999)
    }
}


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

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

@IBAction func switchPressed(sender: AnyObject) {
    toggleSwitch()
}

@IBAction func switchPressedTwo(sender: AnyObject) {
    toggleSwitchTwo()
}



}

Here is the first swift file with the time for first switch:

import Foundation
public extension NSDate {
func xDays(x:Int) -> NSDate {
    return NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: x, toDate: self, options: nil)!
}
var day:            Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay,           fromDate: self).day           }
var month:          Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth,         fromDate: self).month         }
var year:           Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear,          fromDate: self).year          }
var fireDate: NSDate    { return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 8, minute: 0, second: 0, nanosecond: 0)! }
 }

And I would like the next notification time at 1 PM. Here is the swift file I made for that:

 import Foundation
 public extension NSDate {
func xDaysTwo(x:Int) -> NSDate {
    return NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: x, toDate: self, options: nil)!
}
var dayTwo:            Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay,           fromDate: self).day           }
var monthTwo:          Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth,         fromDate: self).month         }
var yearTwo:           Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear,          fromDate: self).year          }
var fireDateTwo: NSDate    { return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 13, minute: 0, second: 0, nanosecond: 0)! }
 }

I am having problems with it and would really appreciate your help, thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
import UIKit

class ViewController: UIViewController {

    @IBOutlet var myDatePicker: UIDatePicker!
    @IBOutlet var mySwitch7am: UISwitch!
    @IBOutlet var mySwitch8am: UISwitch!

    var localNotification7am = UILocalNotification()
    var localNotification8am = UILocalNotification()
    var notificationsCounter = 0

    func datePicker()            { myDatePicker.datePickerMode = UIDatePickerMode.Date }
    func datePickerDefaultDate() { myDatePicker.date = NSDate().hour > 8 ? NSDate().xDays(+1) : NSDate() }

    func notificationsOptions7am() {
        localNotification7am.timeZone = NSTimeZone.localTimeZone()
        localNotification7am.repeatInterval = .CalendarUnitDay
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification7am)
        localNotification7am.alertAction = "Open App"
        localNotification7am.soundName = UILocalNotificationDefaultSoundName
    }
    func notificationsOptions8am() {
        localNotification7am.timeZone = NSTimeZone.localTimeZone()
        localNotification7am.repeatInterval = .CalendarUnitDay
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification8am)
        localNotification7am.alertAction = "Open App"
        localNotification7am.soundName = UILocalNotificationDefaultSoundName
    }
    func toggleSwitch7am(){
        localNotification7am.alertBody = "Here is the seven o'clock notification"
        localNotification7am.fireDate = mySwitch7am.on ? myDatePicker.date.fireDateAt7am         : NSDate().xDays(+36500)
    }
    func toggleSwitch8am(){
        localNotification8am.alertBody = "Here is the eight o'clock notification"
        localNotification8am.fireDate = mySwitch8am.on ? myDatePicker.date.fireDateAt8am         : NSDate().xDays(+36500)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        datePicker()
        datePickerDefaultDate()
        notificationsOptions7am()
        notificationsOptions8am()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    @IBAction func switchedOn7am(sender: AnyObject) {
        toggleSwitch7am()
    }
    @IBAction func switchedOn8am(sender: AnyObject) {
        toggleSwitch8am()
    }
}

Extensions:

    import Foundation
    extension NSDate {
        func xDays(x:Int) -> NSDate {
            return NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: x, toDate: self, options: nil)!
        }
        var day:            Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay,           fromDate: self).day           }
        var month:          Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth,         fromDate: self).month         }
        var year:           Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear,          fromDate: self).year          }
        var fireDateAt7am: NSDate    { return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 7, minute: 0, second: 0, nanosecond: 0)! }
        var fireDateAt8am: NSDate    { return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 8, minute: 0, second: 0, nanosecond: 0)! }
        func fireDateAt(hr:Int, min:Int) -> NSDate {
            return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: hr, minute: min, second: 0, nanosecond: 0)!
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...