0
我是iOS技术的新手。我试图用UILocalNotification构建一个具有swift 2.0的闹钟应用程序。当我点击按钮时,我从UIPickerView获取时间和日期。我也想显示通知和播放声音。 但声音和通知都不起作用。请帮帮我!scheduleLocalNotification在Swift 2.0中不起作用
@IBOutlet weak var myPickerView: UIDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
myPickerView.date = NSDate()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func setAlarmClicked(sender: UIButton) {
let dateFormater : NSDateFormatter = NSDateFormatter()
dateFormater.timeZone = NSTimeZone.defaultTimeZone()
dateFormater.timeStyle = .ShortStyle
dateFormater.dateStyle = .ShortStyle
let dateTimeString : NSString = dateFormater.stringFromDate(myPickerView.date)
print("date is \(dateTimeString)")
self.sheduleLocalNotificationWithDate(myPickerView.date)
}
@IBAction func cancelAlarmClicked(sender: UIButton) {
}
func sheduleLocalNotificationWithDate(fireDate : NSDate){
let localNotification : UILocalNotification = UILocalNotification()
localNotification.fireDate = fireDate
localNotification.alertBody = "Alert !!!!"
localNotification.soundName = "minion.mp3"
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
如果我终止应用程序,它仍然可以工作吗? – Inuyasha
@lnuyasha如果你的意思是通知仍然会触发,那么是的。 – tktsubota
当然,它的工作原理。谢谢!你有我的一天。 – Inuyasha