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.
Related
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.
If it possible to run a service after app was terminated. Please, help me verify following code:
UIApplication* app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
Your app will generally never see willTerminate, because the system generally only terminates your app once it's already suspended (in the background). Once your app is suspended, it gets no further chance to act(*), so there's no callback for that.
The didEnterBackground delegate message or notification should be considered your last chance to clean things up or save state before possible termination.
(*) Okay, your app can do stuff if it's in one of the supported background execution modes, like audio, VoIP, or navigation, but in that case it either hasn't been suspended yet or it's been un-suspended with an entry point specific to that background mode.
I am developing an iOS app which receives apple's remote push notifications at a particular time interval. The notifications are received successfully when app is active as well as when app is in background. But when app is terminated from app switcher/dock, notifications are not received until the app is started again.
I have tried following code to keep app alive in background. This same code is implemented in applicationWillTerminate: method, but it did not work.
__block UIBackgroundTaskIdentifier bgTask ;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
[app enabledRemoteNotificationTypes];
bgTask = UIBackgroundTaskInvalid;
}];
What is the perfect way of receiving notifications even after app is terminated?
If you actively stop the app in the app switcher it stops receiving any push notifications.
This is an intended behaviour as stated by Apple engineers in the developer forums.
i developed an app that makes a http request (async call) when server responses at that time if iPhone is locked. network connection fails, i am getting an error code -1005 "network connection fail". how can i maintain network connection when phone is locked and allow the app to receive response when phone is unlocked again or in background. Also app crashes when user kills app when receiving response.
You should take a look at Background Execution and Multitasking. It allows your app to function up to about 10 minutes while in background.
Here's sample code from the doc:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// 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;
});
}
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.