socket connection killed after iOS app goes to background
Asked Answered
H

3

7

Iam using an iPhone app chat uses socket connection to communicate with the server. When the app is moved to background i can see that the server is able to communicate with the app for about 5 minutes. But after this time, the socket connection is destroyed. But the app stops executing as soon as it moves to background.Why is it that the socket connection is maintained for 5 minutes but not the app execution.Does apple specify the exact time for which the connection would be maintained.

Hyperemia answered 14/2, 2013 at 13:53 Comment(2)
Is your app registered as a VoIP app?Iorgos
nope. it is not registered as Voip.Hyperemia
D
10

You can get a max time of 600 sec(10 min) by using making use of following code in applicationDidEnterBackground:

if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
    UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
    __block UIBackgroundTaskIdentifier background_task; //Create a task object
    background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
        [application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
        background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
        //System will be shutting down the app at any point in time now
    }];
    //Background tasks require you to use asyncrous tasks
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //Perform your tasks that your application requires
        NSLog(@"\n\nRunning in the background!\n\n");
        [application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
        background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
    });
  }
}

Documentation can be found here http://disanji.net/iOS_Doc/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

I just implemented the backgroundTaskIdentifier object and Invalidate the background_task to check the time, app was alive and was running 600sec. You can even get the remaining time by using this

NSLog(@"Time remaining: %f", application.backgroundTimeRemaining);
Despoil answered 14/2, 2013 at 16:41 Comment(3)
If you are targeting iOS 4.3 or later, there is no need for the two initial if statements.Graeme
@chinta Configuring Sockets for VoIP Usage for background can be found here. Very informative wiseman-safiq.blogspot.in/2010/11/…Despoil
now the times is 30 seccondsBrakesman
V
1

From Apple's IOS Programming Guide

Most applications that enter the background state are moved to the suspended state shortly thereafter. While in this state, the application does not execute any code and may be removed from memory at any time. Applications that provide specific services to the user can request background execution time in order to provide those services.

That at least explains, why the app stops executing. Why your server is still able to communicate with your app for 5 minutes may be, because you set an extra long time out and are not closing the socket connection explicitly on your app entering the background.

Vertievertiginous answered 14/2, 2013 at 14:36 Comment(0)
B
0

While the accepted answer is still correct, this was 10 years ago, so it might be relevant to add that nowadays, the maximum you'll get on recent iOS versions is 30 seconds.

Bencion answered 28/3, 2023 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.