lokesh sagiraju
lokesh sagiraju

Reputation: 71

how to call a method by using nstimer in background state

I want to fetch service for every 15 mins,so i am using NSTimer.It is working fine.But how to call same service while app is in background state by using nstimer.Nstimer is not working in background state.Please suggest me.

Upvotes: 0

Views: 379

Answers (1)

Chaman Sharma
Chaman Sharma

Reputation: 636

//when the application is about to move from active to inactive state.

- (void)applicationWillResignActive:(UIApplication *)application
{
    [self sendBackgroundLocationToServer];
}


- (void) sendBackgroundLocationToServer
{
UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
bgTask = [[UIApplication sharedApplication]
          beginBackgroundTaskWithExpirationHandler:^{
              [[UIApplication sharedApplication] endBackgroundTask:bgTask];
          }];


//Start Timer
[self startTimer];

//Close the task
if (bgTask != UIBackgroundTaskInvalid)
{
    [[UIApplication sharedApplication] endBackgroundTask:bgTask];
}
}

Upvotes: 2

Related Questions