So, I am trying to make a UILocalNotification that fires multiple times per instance. That is, user chooses his iteration interval (i.e. every 2 hours) and also chooses the date when the notification would stop (i.e. 01.11.2015.) using the DateTimePicker. I've looked for several answers and in every answers there was always one solution missing and since I am relatively new to iOS Dev, I don't know how to correctly implement them. Most of the issues was the iteration value and the end date triggering properly. Can anyone help?
Asked
Active
Viewed 251 times
2 Answers
0
Use NSTimer
' scheduledTimerWithTimeInterval:target: selector:userInfo:nil repeats:
to trigger and stop notifications based on user's selected values.
-
I was thinking about that, but will the timer run even if the app is in the background? – Petar Sep 16 '15 at 10:36
-
Yes it does as long as timer is initiated on Main thread. Tried & tested :). However, I have edited my post to include a reference link as well. – Abhinav Sep 16 '15 at 10:47
-
I think you only have a limited amount of time that the timer will be allowed to run in the background. – Matt Le Fleur Sep 16 '15 at 10:50
-
@Abhinav , thx, I'll try it, cuz' my colleague told me it has like...18mins of background run time and I was thinking if the notification center can work why can't this? Although it kinda makes sense to make these restrictions. Just imagine if every app had like, 100's of threads...laggy... – Petar Sep 16 '15 at 11:51
0
You would need to use something like NSTimer
to schedule a repeated task like so (the time is in seconds):
var interval = 60.0 // user chosen interval
var helloWorldTimer = NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: Selector("helloWorld"), userInfo: nil, repeats: true)
func helloWorld()
{
println("Hello, World!")
}
Then you would also need to set up another timer similar to above which checks the date (in this example it does so every hour, but you can increase/decrease the accuracy by changing the interval). Once the dates match you then invalidate the previous timer to stop it repeating:
let chosenDate = "01.11.2015" // example date chosen with your DateTimePicker
var dateTimer = NSTimer.scheduledTimerWithTimeInterval(60.0 * 60, target: self, selector: Selector("checkDate"), userInfo: nil, repeats: true)
func checkDate() {
let date = NSDate()
println(date)
let formatter = NSDateFormatter()
formatter.dateFormat = "dd.MM.yyyy"
let formattedDate = formatter.stringFromDate(date)
if formattedDate == chosenDate {
helloWorldTimer.invalidate() // disable previous timer
dateTimer.invalidate() // must also stop this timer as attempting to invalidate the other once already stopped would cause a crash
}
}
n.b. make sure your dates are both in the same format for comparison
n.b.2. this is written using Swift 1.2

Matt Le Fleur
- 2,708
- 29
- 40
-
Nice, I'll see how it works out, I have some bigger implementations on this since I have some passing arguments in this particular class. Thanks and will let ya know how it works. – Petar Sep 16 '15 at 11:52