0

I call one api in applicationDidEnterBackground, but applicationDidEnterBackground method returns after 5 seconds so how could I increase timer or after api finish then only applicationDidEnterBackground will return all of us suggest use beginBackgroundTaskWithExpirationHandler

But I don't know how to use it can anyone guide me?

Here is my code

- (void)applicationDidEnterBackground:(UIApplication *)application
{

    NSString *link=[NSString stringWithFormat:@"http://www.askpundit.com/dev/js_chat/getNewRequest.php?updateStatus=%@&clientid=%@",UpdateStatus,[self getSetting:@"Clientid"]]; 
    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:link]];  

    NSURLConnection *connaction=[[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (connaction)
    {
        responsedata=[NSMutableData data];

        NSLog( @"Data Saved");

    }
} 

Can any one guide me how return applicationDidEnterBackground after my call finish.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Ajay516
  • 45
  • 2
  • 10

1 Answers1

1

As you surmise, beginBackgroundTaskWithExpirationHandler is what you should use. It's very straightforward. This snippet is minimally proofread, and incomplete - but demonstrates the approach.

- (void)applicationDidEnterBackground:(UIApplication *)application {
    _completionTask = [application beginBackgroundTaskWithExpirationHandler:^{
        [application endBackgroundTask:_completionTask];
        _completionTask = UIBackgroundTaskInvalid;
    }];
    // begin your NSURLConnection, etc.
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    if( _completionTask != UIBackgroundTaskInvalid ) {
         [application endBackgroundTask:_completionTask];
         _completionTask = UIBackgroundTaskInvalid;
}
FluffulousChimp
  • 9,157
  • 3
  • 35
  • 42