import UIKit
class MyView: UIViewController {
@IBOutlet var mySwitch: UISwitch!
@IBOutlet var myDatePicker: UIDatePicker!
func datePicker() { myDatePicker.datePickerMode = UIDatePickerMode.Date }
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
datePicker()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func switchPressed(sender: AnyObject) {
if mySwitch.on{
var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "Open App"
localNotification.alertBody = "Here is your notification at 7:00 AM"
localNotification.fireDate = myDatePicker.date
localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
} else {
var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "Open App"
localNotification.alertBody = "This notification should not appear"
localNotification.fireDate = NSDate(timeIntervalSinceNow: 999999999999)
IApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
}
}
Asked
Active
Viewed 3,202 times
0

Leo Dabus
- 229,809
- 59
- 489
- 571

Syed Arafat Qureshi
- 147
- 3
- 13
-
What, exactly, is your question? If it's "how do I use a date picker", there are plenty of tutorial resources out there which will help with that. – dpassage Dec 07 '14 at 21:53
-
No, sorry for not being clear, but I meant how do i connect my date picker to the fire date? I want the notification to appear every day at that time set. – Syed Arafat Qureshi Dec 07 '14 at 22:14
2 Answers
1
//
// ViewController.swift
// Combining Date and Time
//
// Created by Leonardo Savio Dabus on 08/12/2014.
// Copyright (c) 2014 inDabusiness.com. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
// IBOutlet goes here
@IBOutlet var myDatePicker: UIDatePicker!
@IBOutlet var mySwitch: UISwitch!
// let = whatever goes here
// var = whatever goes here
var localNotification = UILocalNotification() // You just need one
var notificationsCounter = 0
// put your functions now
func datePicker() { myDatePicker.datePickerMode = UIDatePickerMode.Date }
func datePickerDefaultDate() { myDatePicker.date = NSDate().xDays(+1) }
func notificationsOptions() {
localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.repeatInterval = .CalendarUnitDay
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
localNotification.alertAction = "Open App"
localNotification.alertBody = "Here is the seven o'clock notification"
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
// you may add arbitrary key-value pairs to this dictionary.
// However, the keys and values must be valid property-list types
// if any are not, an exception is raised.
// localNotification.userInfo = [NSObject : AnyObject]?
}
func toggleSwitch(){
if mySwitch.on{
localNotification.fireDate = myDatePicker.date.fireDate // combined date = picked Date + 7:00am time
} else {
localNotification.fireDate = NSDate(timeIntervalSinceNow: 999999999999) // will never be fired
}
}
override func viewDidLoad() {
super.viewDidLoad()
datePicker()
datePickerDefaultDate()
notificationsOptions()
// 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.
}
// here is where you place your IBActions
@IBAction func switchPressed(sender: AnyObject) {
toggleSwitch()
}
}
create a new Swift Source file at your project to put your extensions
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: 7, minute: 0, second: 0, nanosecond: 0)! }
}

Leo Dabus
- 229,809
- 59
- 489
- 571
-
This is useful! However as I am getting an error from the public extension NSDate saying: declaration is only valid at file scope. @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 00:22
-
-
-
Look at the new edit for the question please @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 00:29
-
-
I know its tedious helping someone like me but its great people like you who make me and others better programers @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 00:32
-
Right, now I have the new Swift file with your code, what do I do next? @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 00:36
-
-
You have to learn where to place your elements. You are mixing everything – Leo Dabus Dec 08 '14 at 01:15
-
Right. So I am trying to make it so the notification happens at 07:00 am every day but only if the switch is pressed on. I will send you the file if you tell me how to, thank you. @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 01:18
-
Using your Finder, right-click you project folder and select "Compress yourProjectFolder". You can use dropbox.com – Leo Dabus Dec 08 '14 at 01:20
-
-
I'm just about to send it, the notifications come in every minute instead of every day now. Just hang on, almost done – Syed Arafat Qureshi Dec 08 '14 at 01:36
-
Thats easy to fix. localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay – Leo Dabus Dec 08 '14 at 01:38
-
-
Test it too! It seems to not be working right but with your programming skills I'm sure you can put it right! @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 01:41
-
-
Have you found whats wrong with the code? @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 01:51
-
-
-
Funny the problem was the constraints I've added for the positioning – Leo Dabus Dec 08 '14 at 01:57
-
I didn't know constraints were that big an issue, but wow, I'm surprised! @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 01:59
-
-
-
Will it appear every day from tomorrow until switch is turned off? @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 02:03
-
Great! Can I look at it please? Also can I add to this later by making an afternoon one? @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 02:06
-
I have already finished the date part I will send you what I have done so far – Leo Dabus Dec 08 '14 at 02:10
-
-
I forgot to call the day + 1 to ajust the default date. let me fix 1 second – Leo Dabus Dec 08 '14 at 02:18
-
I just saw another problem the interval was set to second I changed to day – Leo Dabus Dec 08 '14 at 02:22
-
Okay, so now I will get a notification every day? @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 02:23
-
-
-
When I was testing the last one it kept appearing every minute, I hope this doesn't happen. @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 02:27
-
-
Oh, okay, I will test this then, I will update on here if I have any troubles, thank you for your help!! Also, can I add new times in by re-using the code? – Syed Arafat Qureshi Dec 08 '14 at 02:29
-
I think you also have to set the notifications timezone to local time zone – Leo Dabus Dec 08 '14 at 02:36
-
-
The date specified in fireDate is interpreted according to the value of this property. If you specify nil (the default), the fire date is interpreted as an absolute GMT time, which is suitable for cases such as countdown timers. If you assign a valid NSTimeZone object to this property, the fire date is interpreted as a wall-clock time that is automatically adjusted when there are changes in time zones; an example suitable for this case is an an alarm clock. – Leo Dabus Dec 08 '14 at 02:39
-
What would I need to put in the code? And where will I need to put it? @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 02:41
-
-
Do I put this in togleSwitch function? @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 02:44
-
-
Alright, in the future, can I contact you again and can you put in more switches that trigger at another time please but still linked to date from date picker please? @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 02:49
-
can you help me to make another notification appear when another switch is activated please? – Syed Arafat Qureshi Dec 08 '14 at 19:41
-
just add another switch, connect the outlet at the top of the other, create another almost identical function named toggle2 or whatever and change only what needs to be changed or open another question that I will gladly help you – Leo Dabus Dec 08 '14 at 19:42
-
I think you should also suggest tomorrow date as fireDate only if localtime is already after 7am – Leo Dabus Dec 08 '14 at 19:44
-
Could you re-make the app again for me? I am finding it so hard to use these notifications but with other things, I am finding it easy. @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 19:51
-
Look at the question again, I re-edited it with the code I have now. The thing is I don't actually know what to change. @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 19:55
-
Look your question should be used by future visitors as a reference, if you change the question id doesn't make any sense anymore. You should revert your edit and copy your code into a new question, showing what you tried, the error you got and how you are supposed to fix it. This way I am helping also everybody else in the world, not only you. – Leo Dabus Dec 08 '14 at 19:57
-
-
So I can have another time for the notification @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 20:04
-
Sure there is already 60 comments here. open another question and revert you edits here. – Leo Dabus Dec 08 '14 at 20:07
-
Sorry @LeonardoSavioDabus , I changed it back, and here is the link to the new question http://stackoverflow.com/questions/27366127/setting-multiple-times-for-notifications-in-swift – Syed Arafat Qureshi Dec 08 '14 at 20:18
-
My new question is here @LeonardoSavioDabus :http://stackoverflow.com/questions/27366127/setting-multiple-times-for-notifications-in-swift – Syed Arafat Qureshi Dec 08 '14 at 21:01
-
-
@LeoDabus this ans is not compatible with swift 2.2, getting error **NSCalendarUnit has no member** kindly help if you can i am trying to create custom notification with given time and date. – vaibhav Oct 20 '16 at 07:47
-
@Arun I could post a Swift 4 version but using UserNotifications you wouldn't be able to set a fire date. It would schedule a daily notification starting at the next matching date or you can schedule a single future date notification (not daily). – Leo Dabus Nov 21 '17 at 20:13
-
@LeoDabus thanks a lot, Can you please update, I will update the code for daily notification – jerfin Nov 22 '17 at 04:30
0
If you already have the UIDatePicker
, all you need to do is grab the date
property from it and use it to set the fireDate
.

Dima
- 23,484
- 6
- 56
- 83
-
So how would i actually do this? I can make the date picker display just the time. Now how do i put the time into the fireDate? – Syed Arafat Qureshi Dec 07 '14 at 22:01
-
-
I tried that @LeonardoSavioDabus i got an error for some reason, look at the comment below for what i have now and advise me on how to change it please, thank you. – Syed Arafat Qureshi Dec 07 '14 at 23:44
-
-
-
What time is it happening every day? You need to fire it using any date with a 7am hour and keep the repeating once a day – Leo Dabus Dec 07 '14 at 23:56
-
I would like that to be set by the date picker @LeonardoSavioDabus – Syed Arafat Qureshi Dec 07 '14 at 23:57
-
So you can let the user select only a date and manually input 7am to that date – Leo Dabus Dec 07 '14 at 23:59
-
No no, I'm so sorry for my bad english, I want the notification to appear every day at 7 am @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 00:00
-
Did you understand? You have to fire it at the desired date at 7am and repeat it every day as you already have done so far – Leo Dabus Dec 08 '14 at 00:00
-
OH! How do I do that? I'm sorry asking so many questions, but thank you for being so patient and helping @LeonardoSavioDabus – Syed Arafat Qureshi Dec 08 '14 at 00:02
-
how do you set the date picker to pick only the Date or how do you create a Date Object using the selected date but with a specific time (7am) or both of them? – Leo Dabus Dec 08 '14 at 00:04
-
-
-