Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep iphone ios websocket connection alive while in the background?

I created a chat application using SocketRocket,when I quit the app(enter background mode) I want to receive the chat message! so i could do that by calling it a VoIP app but some people says Apple will reject it from the App Store unless it also truly does VoIP. So how can I do that and if the only way to do that is by calling it a VoIP app how does whatsapp handles this situation??

Here's what i did to keep the app alive in the background:

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


 NSLog(@"Application entered background state.");
    NSAssert(self.backgroundTask == UIBackgroundTaskInvalid, nil);
    self.didShowDisconnectionWarning = NO;

self.backgroundTask = [application beginBackgroundTaskWithExpirationHandler: ^{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"Background task expired");
        if (self.backgroundTimer)
        {
            [self.backgroundTimer invalidate];
            self.backgroundTimer = nil;
        }
        [application endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    });
}];

dispatch_async(dispatch_get_main_queue(), ^{
    self.backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(timerUpdate:) userInfo:nil repeats:YES];
});

}


- (void) timerUpdate:(NSTimer*)timer {
    UIApplication *application = [UIApplication sharedApplication];

    NSLog(@"Timer update, background time left: %f", application.backgroundTimeRemaining);

    if ([application backgroundTimeRemaining] < 60 && !self.didShowDisconnectionWarning)
    {
        UILocalNotification *localNotif = [[UILocalNotification alloc] init];
        if (localNotif) {
            localNotif.alertBody = @"Background session will expire in one minute.";
            localNotif.alertAction = @"OK";
            localNotif.soundName = UILocalNotificationDefaultSoundName;
            [application presentLocalNotificationNow:localNotif];
        }
        self.didShowDisconnectionWarning = YES;
    }
    if ([application backgroundTimeRemaining] < 10)
    {
        // Clean up here
        [self.backgroundTimer invalidate];
        self.backgroundTimer = nil;

        [application endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    }

}
like image 273
M.Alatrash Avatar asked Jun 28 '14 15:06

M.Alatrash


People also ask

How do you keep WebSockets alive?

TCP/ Websocket Keepalive PING Example util. Timer. The above method will start a timer task to send ping message for every 5 seconds to the server to keep the websocket connection keepalive. Therefore, the websocket connection will not get closed even after 15 seconds timeout period.

Can iOS app run in background?

No. Applications cannot run in the background for over 10 minutes, except for a few certain situations (VOIP, playing audio, etc.) An iOS app which plays audio will keep playing audio indefinitely in the background so long as the audio is playing.

Do WebSockets work on iOS?

On the iOS platform, it is possible – thanks to WebSockets.

Do mobile apps use WebSockets?

Definitely, a WebSocket web app will run on any HTML5-compliant browser, including mobile browsers such as Safari for iOS and Chrome for mobile.


1 Answers

I believe your only option is to use remote notifications. You need your app to register for them, and your server to detect when your app is not running (i.e. connected via the socket), and then send a notification instead.

like image 162
jcaron Avatar answered Sep 18 '22 20:09

jcaron