NStimer in Background

Discussion in 'iOS Programming' started by erdinc27, May 9, 2013.

  1. erdinc27 macrumors regular

    Joined:
    Jul 20, 2011
    #1
    hey all. i want to develop an application which works like an alarm logic. i trigger the method which calculates the time with a timer but the problem is when i take the application to background the timer doesnt work. here my code is
    Code:
    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"BackgroundNotification" object:self];
    }
    
    - (void)applicationWillEnterForeground:(UIApplication *)application
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ForegroundNotification" object:self];
    }
    
    ViewController.m
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkTheIftarTime) userInfo:nil repeats:YES];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(theAppInBackground) name:@"BackgroundNotification" object:nil];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(theAppIsActive) name:@"ForegroundNotification" object:nil];
    }
    
    - (void)theAppInBackground
    {
        [myTimer invalidate];
        myTimer=nil;
    }
    
    - (void)theAppIsActive
    {
        self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkTheTime) userInfo:nil repeats:YES];
    }
    
    how i can keep calculating the time when the application is in background?
     
  2. xArtx macrumors 6502a

    Joined:
    Mar 30, 2012
    #2
    By calculating the difference in system time.
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
  4. lastcall macrumors member

    lastcall

    Joined:
    Jan 10, 2013
    #4
    You didn't post code to the "checkTheIftarTime" method.

    If you want your timer to persist, even if your app crashes or exits, then I would store the start time somewhere, preferrably in NSUserDefaults.
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    Timers don't run in the background unless your code creates a background task (UIBackgroundTask). Background tasks only run for about ten minutes. There are a few types of apps that can get around these restrictions but apple is strict about enforcing those restrictions.

    Look at local notifications or even remote notifications if you want your app to be called back at a time that is more than ten minutes in the future.
     
  6. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #6
    Maybe you need to look at getting the system to notify you when the time is up.
     
  7. erdinc27 thread starter macrumors regular

    Joined:
    Jul 20, 2011
    #7
    thanks for the helps. i used local notifications as phone developer suggested
     

Share This Page