3

I just went through trying to make an alarm clock app with local notifications but that didn't do the trick because I needed an alert to appear instead of it going into notification centre in iOS 5+

So far I've been struggling greatly with nstimer and its functions so I was wondering if anyone could help out.

I want when the user selects a time (through UIDatePicker, only time) for an alert to be displayed at exactly this time. I have figured out how to get the time from UIDatePicker but I do not know how to properly set the firing function of nstimer. This is what I have attempted so far, if anyone could help... be much appreciated. Thank you

Example (it keeps going into the function every second opposed to a certain time I told it too... not what I want):

NSDate *timestamp;
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];

[comps setHour:2];
[comps setMinute:8];
timestamp = [[NSCalendar currentCalendar] dateFromComponents:comps];

NSTimer *f = [[NSTimer alloc] initWithFireDate:timestamp
                             interval:0
                               target:self
                             selector:@selector(test)
                             userInfo:nil repeats:YES];

NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:f forMode: NSDefaultRunLoopMode]; 
Alex G
  • 2,299
  • 5
  • 37
  • 54
  • @Nate but what if the user chooses they want to repeat the alarm? and I don't think that makes sense since it is repeating every second and not repeating at the time I set (2:08)? – Alex G Jul 02 '12 at 09:18
  • for an alarm app, you can't know at the start whether the user will repeat (snooze) or not. so, you can't make that decision when you first create the timer. when the alarm goes off (`test`), then you'll need to decide whether to create a new timer, with the snooze *interval* – Nate Jul 02 '12 at 09:20
  • @Nate Okay fair enough thank you but that doesn't address the fact its firing every second and repeating... why isn't it doing this at 2:08 and not every single second? – Alex G Jul 02 '12 at 09:22
  • actually, you've done something very strange. you've set repeats = YES, but interval = 0. that doesn't really make much sense. so, maybe the timer is defaulting to some nonzero time, to avoid burning up the CPU with a huge number of calls to `test` with no time in between. – Nate Jul 02 '12 at 09:23
  • I guess if the **snooze** button only allows you to snooze for one particular amount of time (for example, 10 minutes), you could set the timer as you've done, with a fire date equal to the time it should go off. the interval would then be 10 minutes. and the repeats could be YES. then, you could simply cancel the timer if the user decides **not** to snooze. that could work. – Nate Jul 02 '12 at 09:26
  • @Nate wait so interval is the time between when NStimer receives the task to when it actually launches the method choosen? – Alex G Jul 02 '12 at 09:30
  • No. Not between when it receives the task and calling `test`. The `test` method gets called at the fire date. And if repeats = YES, then it will get called again after `interval`. See [the apple docs for NSTimer here](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html) – Nate Jul 02 '12 at 09:35

1 Answers1

2

As @Nate said change

NSTimer *f = [[NSTimer alloc] initWithFireDate:timestamp
                             interval:0
                               target:self
                             selector:@selector(test)
                             userInfo:nil repeats:YES];

To

NSTimer *f = [[NSTimer alloc] initWithFireDate:timestamp
                             interval:0
                               target:self
                             selector:@selector(test)
                             userInfo:nil repeats:NO]; //<-- Change here
Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
  • Thanks Jennis but what if the user chooses they want to repeat the alarm? and I don't think that makes sense since it is repeating every second and not repeating at the time I set (2:08)? – Alex G Jul 02 '12 at 09:19
  • So what you want is if you set 2:08 alarm that should be repeated for days ? – Janak Nirmal Jul 02 '12 at 09:21
  • Yes. Im saying the 2:08 just as an example right now, the actually time will be decided by the user. Thanks – Alex G Jul 02 '12 at 09:22
  • In that case you need to look into `UILocalNotification` Tutorials because when your application goes in background or closed NSTimer will not work as required. – Janak Nirmal Jul 02 '12 at 09:26
  • really? if its suspended in background nstimer will not work? And for the app being closed, I have warned my users to not do so due to apples restrications – Alex G Jul 02 '12 at 09:27
  • @AlexG, Jennis is right. Once you go into the background, your `NSTimer` objects will stop firing. So, `NSTimer` won't work for you, if your app is supposed to be opened to set the alarm, then closed for a long time ... with the alarm going off later. – Nate Jul 02 '12 at 09:32
  • @Nate... hmmm have to rethink my approach. My buddy told me that that I couldn't get a uialertview to appear in iOS 5 instead of it going into notification centre. Is this true? – Alex G Jul 02 '12 at 09:34
  • Your approach should be like scheduling local notification when user sets alarm. And to have UIAlertView for push notification in iOS5 its user's choice and can be changed through Settings->Notification->Your application->Notification Center. If this is on than notification goes in notification center. Otherwise it will prompt you alert. – Janak Nirmal Jul 02 '12 at 09:39
  • @AlexG, As Jennis said ... unfortunately, there doesn't seem to be a way for you to specify the **alert** style of notification for your app. The user can go into Settings and pick that style (vs. banner style). See [this stack overflow question](http://stackoverflow.com/questions/8229385/set-default-ios-local-notification-style-for-application) for a discussion of this topic. – Nate Jul 02 '12 at 09:42
  • @Jennis Thanks guys, one last question and I'll accept... how does apple do it with there alarm clock app – Alex G Jul 02 '12 at 09:53
  • @Nate Thanks guys, one last question and I'll accept... how does apple do it with there alarm clock app – Alex G Jul 02 '12 at 09:55
  • @AlexG, They are Apple, and therefore let themselves do things that 3rd-party apps can't :). It's not fair, I know! – Nate Jul 02 '12 at 09:57
  • They are using Local notification. i.e. lets say you have set fire date. They set Local notification for fire date and that's it. In their notification they have only 1 button of ok when u see notification. But in our case we also have to use local notification but we will have 2 buttons on is ok and other is view notification if alert is displayed. – Janak Nirmal Jul 02 '12 at 09:57