1

I am developing an alarm application. I use NSTimer in my application. The timer works just fine. But when I minimize the application, the timer stops working. How to make it work while the application is closed or minimized?

Here is my code

- (void) alarmSaveButtonAction:(id)sender {


timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
                                       selector:@selector(onTimerEvent)
                                       userInfo:nil repeats:YES];  
}
-(void)onTimerEvent
{
    NSLog(@"saravanan");
    NSDate *now = [NSDate new]; 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"hh:mm:ss a";
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    NSLog(@"The Current Time is %@",[dateFormatter stringFromDate:now]);
    NSString *string2 = [NSString stringWithFormat:[dateFormatter stringFromDate:now]];
    NSLog(@"currenttime %@", string2);
    [dateFormatter release];


    Resource *resourceLoader = [[Resource alloc] init];

    NSDictionary *prefDic = [resourceLoader getPlistDataAsDictionary:@"preference"];

    NSDate *selectedDateTime = [[NSDate alloc]init];

    selectedDateTime = [prefDic objectForKey:@"alarmtime"];

    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    dateFormatter1.dateFormat = @"hh:mm:ss a";
    [dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
    NSLog(@"saravanan date %@",[dateFormatter1 stringFromDate:selectedDateTime]);
    NSString *string1 = [NSString stringWithFormat:[dateFormatter1 stringFromDate:selectedDateTime]];
    NSLog(@"currenttime %@", string1);
    [dateFormatter1 release];


    if ([string1 isEqualToString:string2]) 
    {
        NSLog(@"alarm scheduld");
        [self scheduledNotification];
        [timer invalidate];
        timer = nil;
    }
}

Thanks in advance.

John Smith
  • 2,012
  • 1
  • 21
  • 33
iSara
  • 919
  • 3
  • 10
  • 24
  • @SavaMazăre Please try to be polite and constructive in edit comments. Are you a better speaker of this poster's language than they are of English? If not, refrain from comment about their language use and stick to describing your edits in your comment. – Craig Ringer Aug 20 '12 at 06:51
  • @BJH: Bad idea. The "solution" described there is a violation of the app store rules. – Lily Ballard Aug 20 '12 at 06:52
  • Thanks @KevinBallard...thought it was a duplicate. – JAB Aug 20 '12 at 06:54

2 Answers2

2

For alarm use scheduled local notifications.

Dmitry Shevchenko
  • 31,814
  • 10
  • 56
  • 62
1

You can't do this, at least not while staying within the bounds of both public API and the rules of the app store.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • if any other way possible to do this, each and every seconds i need to call -(void)onTimeEvent method even app goes to minimize(sleep mode or we went some other app), and running. – iSara Aug 20 '12 at 06:58