0

client :in plist file Required background modes:

[App plays audio or streams audio/video using AirPlay,App provides Voice over IP services]

in function createStream

 CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
    CFWriteStreamSetProperty(writeStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);

in function applicationDidEnterBackground

BOOL backgroundAccepted = [application setKeepAliveTimeout:600 handler:^{
    [self send:[NSString stringWithFormat:@"%@ KeepAliveTimeout",[NSDate date]]];
}];
if (backgroundAccepted)
{
    DLog(@"VOIP background");
}
_bgTaskId=[application beginBackgroundTaskWithExpirationHandler:^{
    [self alert:@"ExpirationHandler"];
    [application endBackgroundTask:_bgTaskId];
    _bgTaskId=UIBackgroundTaskInvalid;
}];

in function applicationWillEnterForeground

[application clearKeepAliveTimeout];

server send message to client every 5 senconds with NSTimer.client app use local notification to show message in background immediately;

2 Answers2

0

If your app dose wake up more than 15 times in a 300 seconds interval is going to be killed. if you send a message to the app every 5 second and process this message then i am surprised that your app runs for 9 minutes.

Have a look here: SO1 SO2

Community
  • 1
  • 1
alinoz
  • 2,822
  • 22
  • 38
0

I have go through exactly problem like you, all you have to do is remove the code

_bgTaskId=[application beginBackgroundTaskWithExpirationHandler:^{
    [self alert:@"ExpirationHandler"];
    [application endBackgroundTask:_bgTaskId];
    _bgTaskId=UIBackgroundTaskInvalid;
}];

I have try it several times, If you call beginBackgroundTaskWithExpirationHandler, your application will be terminate by system anyway.

and just keep the code BOOL backgroundAccepted = [application setKeepAliveTimeout:600 handler:^{ [self send:[NSString stringWithFormat:@"%@ KeepAliveTimeout",[NSDate date]]]; //unless you have more job to do here. }]; if (backgroundAccepted) { DLog(@"VOIP background"); }

From the result I can see, If you already keep the right handle for Socket, Your handle code will be call automatically, just like you are in the foreground.

Yingsong
  • 76
  • 3