Note that there are some explanatory texts on larger screens.

plurals
  1. POHow would I make my timer run in the background and then send a local notification?
    primarykey
    data
    text
    <p>For some reason my timer only runs in the foreground. I've searched here for solutions but I couldn't find any good ones. My timer uses Core Data and my timeInterval is saved after every decrement. Also, I'm sending a UILocalNotification but that doesn't work. I'm assuming it doesn't send a alert view if its in the foreground..Well this is what I have now:</p> <pre><code>-(IBAction)startTimer:(id)sender{ if (timer == nil) { [startButton setTitle:@"Pause" forState:UIControlStateNormal]; timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES]; } else { [startButton setTitle:@"Resume" forState:UIControlStateNormal]; [timer invalidate]; timer = nil; } } -(void)timerAction:(NSTimer *)t { if(testTask.timeInterval == 0) { if (self.timer) { [self timerExpired]; [self.timer invalidate]; self.timer = nil; } } else { testTask.timeInterval--; NSError *error; if (![self.context save:&amp;error]) { NSLog(@"couldn't save: %@", [error localizedDescription]); } } NSUInteger seconds = (NSUInteger)round(testTask.timeInterval); NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u", seconds / 3600, (seconds / 60) % 60, seconds % 60]; timerLabel.text = string; NSLog(@"%f", testTask.timeInterval); } -(void)timerExpired{ UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.alertBody = @"Time is up"; localNotification.alertAction = @"Ok"; localNotification.timeZone = [NSTimeZone defaultTimeZone]; [[UIApplication sharedApplication]presentLocalNotificationNow:localNotification]; } </code></pre> <p>I would really appreciate some guidance to making my timer work in the background (just like Apple's timer). I'm not sure how I would use NSDateComponents since I also need the testTask.timeInterval to be updated even when the application in the background..</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload