iOS PjSip - background working - ios

Recently I have to implement background working app with voip. For that I use PJSip.
Right now I have done registering and handling calls working perfectly when app is running.
When app goes to the background, by first 10 minuts works fine -> new incomming calls are captured and are send as local notifications - so this is fine. After 10 minutes sip stops working and incomming calls doesn't arrives as well.
Could you help me with this?
I have check "bacground working - VoiP"
I have done keep alive in pjsip from siphon2 example application.
I have also:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[PjSipService service] setIsInBackground:YES];
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
int KEEP_ALIVE_INTERVAL = 600;
[self performSelectorOnMainThread:#selector(keepAlive) withObject:nil waitUntilDone:YES];
NSLog(#"startingKeepAliveTimeout1");
[application setKeepAliveTimeout:KEEP_ALIVE_INTERVAL handler: ^{
NSLog(#"startingKeepAliveTimeout2");
[self performSelectorOnMainThread:#selector(keepAlive) withObject:nil waitUntilDone:YES];
}];
__block UIBackgroundTaskIdentifier bgTask;
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//[self deregis];
[application endBackgroundTask: bgTask]; //End the task so the system knows that you are done with what you need to perform
bgTask = UIBackgroundTaskInvalid; //Invalidate the background_task
NSLog(#"\n\nRunning in the background!\n\n");
});
}
"keepAlive" function is empty.
I've read that for this function keepAlive is enought - but without background task it doesn't work even 10 minuts.
Application has to works in iOS7 and iOS6

Apps running in background, is performed using setKeepAliveTimeout method in IOS. But from later versions, setKeepAliveTimeout method is deprecated.
You want to use Remote notifications to receive incoming call. particularly for VOIP incoming calls, apple introduced Callkit framework. please follow that framework to accept incoming calls while your VOIP App is in background state.
https://www.raywenderlich.com/150015/callkit-tutorial-ios

I am working on the same project with same requirements and I came across with the same issue recently. I tried many different solutions and finally I found something that is working for now.
Please note that I recently found this solution and I am working on this. I haven't consider the reaction of this code at least for now.
Sample Code :
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self sipConnect];
backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundTask];
}];
}
-(void) endBackgroundTask
{
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}

You probably use UPD. To make sure you use TCP, when setting up your registrar you need to specify something like this sip:ip_address:port;transport=tcp

Apple has deprecated keepAliveTimeoutHandler. To receive calls when applicationDidEnterBackground you have to use PushKit framework. Read documentation for more information here.

Related

Application is being closed automatically after iPad locked

I have enabled Background Fetch from capabilities.
Please give me solution that once user open the application, it should not closed automatically.
Adding UIBackgroundModes does not make you app keep running in background.
Each of the preceding modes lets the system know that your app should be woken up or launched at appropriate times to respond to relevant events. For example, an app that begins playing music and then moves to the background still needs execution time to fill the audio output buffers. Enabling the Audio mode tells the system frameworks that they should continue to make the necessary callbacks to the app at appropriate intervals. If the app does not select this mode, any audio being played or recorded by the app stops when the app moves to the background.
You will have to either have a task running or callbackHandlers to respond when OS send any signal from the background task as shown in example from Apple
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:#"MyTask" expirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
Please follow Background Execution for more details.

iOS background fetch before termination

I have a situation where I must call a web service before app termination but not when enters the background, because this should be done only once which when the app get terminated.
I tried the background execution thing but I believe it won't work once the app get terminated but only if the app in the background.
Here is my latest try inside applicationWillTerminate:
__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithName:#"MyTask" expirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
// Call web service here
[application endBackgroundTask:bgTask];
bgTask =UIBackgroundTaskInvalid;
});
You can't do that, and honestly, why would you?
This is an ill-posed problem anyway. What should happen if the battery dies? What if the app crashes? What if the app goes to background and then the OS terminates it when memory pressure increases? What if you lose internet access and the app is terminated?
The closest thing you can do is to implement some sort of heartbeat service which your app regularly pings. If the pinging stops, your app has either terminated or network connectivity was lost.

Pubnub connection stops when application goes into background state iOS 8

Title says it all.
I've looked at this question and also here on the pubnub forums (same question, just different suggestion).
The core of the issue is that as soon as the application suspends, pubnub connectivity is queued and not sent until the app goes back to foreground. It seems to be a reasonable thing to do to send a notification saying that you're going in the background on your channel but it doesn't work.
From my readings I understand that pubnub uses websockets and that it is not allowed in background mode. Even tried to enable VOIP as a background mode with no luck but Location updates bg mode works. However, using this will have my app rejected as I don't use location services.
When running this code
- (void)applicationDidEnterBackground:(UIApplication *)application {
[PubNub sendMessage:#"Hello from PubNub iOS!" toChannel:self.myChannel;
}
I get this log entry from pubnub (so at least I know the command is ran):
Looks like the client suspended"; Fix suggestion="Make sure that your application is configured to run persistently in background
I have been killing myself over this for a day. One of these days where you start doing something that you think is pretty simple, a 15min thing and it turns into a day of frustration ... You know what I mean :)
I was actually able to send the messages I needed when the app was about to enter Background. And without enabling any of the background modes.
I took advantage of the background finite task as explained is this tutorial.
- (void)applicationWillResignActive:(UIApplication *)application {
[PubNub updateClientState:#"MyID" state:#{#"appState":#"Background",#"userNickname":#"MyNickname"} forObject:[PNChannel channelWithName:#"MyChannel"]];
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(#"Background handler called. Not running background tasks anymore.");
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid;
}];
}
And implementing the stop background when coming back online
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (self.backgroundTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid;
NSLog(#"Task invalidated");
}
}

Location update background mode app getting rejected

I have done an app using location update as background mode, and I am updating my location after each keepAlive interval using webservice. I am using the below mentioned code.
if ([application respondsToSelector:#selector(setKeepAliveTimeout:handler:)])
{
[application setKeepAliveTimeout:120 handler:^{
DDLogVerbose(#"KeepAliveHandler");
// Do other keep alive stuff here.
}];
}
self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(#"ending background task");
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}];
self.backgroundTimerForLocationUpdate = [NSTimer scheduledTimerWithTimeInterval:120
target:self
selector:#selector(changeAccuracy)
userInfo:nil
repeats:YES];
and in the location delegate I am calling a web request for updating the location in server, to track the user.
Will apple reject the app for using the location update background mode.
Will apple reject the app for using the location update background mode.
According to the Apple Documentation you are only allowed to if your iOS App
Playing and Recording Background Audio An app that plays or records audio continuously (even while the app is running in the background) can register to perform those tasks in the background.Tracking the User’s Location and Implementing a VoIP Client (Voice over Internet Protocol). So Accordingly no problem at all but the final result will be declared by Apple Review Team
Good Luck
Apple will reject application if you start or stop location update while application is in background.
But you can get latest location update in background with method "startMonitoringSignificantLocationChanges". It will update current location value when app is in background and when you will come in foreground mode, You will have latest location information to perform any subsequence activity.

How to keep a block of code alive in background?

I have searched for this topic bu mostly i face the restrictions of Apple. I need to once per minute control my server and ifthere is a change, fire a local notification. What i need is, how to keep timer(NSTimer) alive in background(or when the device lock is activated..) Any idea please. Thanks
You could do your logic in the server part and if there are changes send a Push Notification.
You need to reconsider the design of your app. You can't guarantee that your app will never be killed when the OS goes out hunting for memory to free up. What happens in that scenario? Push Notifications are your best bet here. First of all, you don't need to be polling your server every 60 seconds; you just fire a notification when the content you're interested in changes on the server. Secondly, the notification will be received even if your app isn't running.
The other issue is that you have to tell Apple, via your info.plist, which background modes your app supports. This is really for apps that run music or VoIP in the background. Polling a web server is not one of those supported modes. With push notifications, you also get some delegate methods you can use to handle the information passed in through the notification when the app enters the foreground.
My app continuously runs in the background with following piece of code.....
-(void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask = 0;
backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(backgroundTask) userInfo:nil repeats:YES];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
}
Now do whatever you want in the backgroundTask method.

Resources