问题
I am in the process of writing a VoIP application for iOS but when App is in background it stops accepting calls. When the app is active again all the queued up messages start getting processed.
The following is what I have done.
When building the app I add Voice over IP as well as Audio and AirPlay to the plist file. Then I mark the websocket connection with NetworkServiceTypeVoIP
as you can see here.
I have not set the keep alive timeout handler because registration doesn't matter if the app won't wake up to answer the call. Any help would be greatly appreciated.
It should be noted that this is my first Swift project and I'm not very familiar with the iOS platform.
回答1:
To allow to work your app in background mode, you need to enable Voice over IP flag ON(Path : Go to target --> capabilities --> Background Modes). like as below.

And add following code in your project to support in background:
Step 1: Declare __block UIBackgroundTaskIdentifier bgTask as global variable.
Step 2: To add following code in applicationDidEnterBackground.
- (void)applicationDidEnterBackground:(UIApplication *)application {
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
bgTask = UIBackgroundTaskInvalid;
}];
}
Step 3: Stop background task handler once apps come in foreground mode.
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}
回答2:
Take a look at Apple documentation, you're probably interested in UIBackgroundModes=voip
.
UPD:
From documentation:
To configure a VoIP app, you must do the following:
- Enable support for Voice over IP from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the
UIBackgroundModes
key with the voip value in your app’s Info.plist file.)- Configure one of the app’s sockets for VoIP usage.
- Before moving to the background, call the
setKeepAliveTimeout:handler:
method to install a handler to be executed periodically. Your app can use this handler to maintain its service connection.- Configure your audio session to handle transitions to and from active use.
回答3:
You may find your answer or some clue --> here <--
Properly have to research the following:
applicationDidEnterBackground
beginBackgroundTaskWithExpirationHandler
来源:https://stackoverflow.com/questions/28028682/ios-backgrounding-not-working