Friday 6 June 2014

scheduled timer in background in iphone sdk


UIBackgroundTaskIdentifier backgroundTask;

timer = [NSTimer scheduledTimerWithTimeInterval:1200.0
                                                        target:self
                                                      selector:@selector(runTimer)
                                                      userInfo:nil
                                                       repeats:YES];
   backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"Background handler called. Not running background tasks anymore.");
        [[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
       backgroundTask = UIBackgroundTaskInvalid;
    }];

-(void)runTimer{
}

-(void)stopTimer{
 [timer invalidate];
    timer = nil;
    if (self.backgroundTask != UIBackgroundTaskInvalid)
    {
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    }
}

No comments:

Post a Comment