Application delegates are not calling while application entered into background - iphone

I am implementing an iPhone application.I am downloading data from server by using asynchronous request in the application.If, I Press the home button, My application delegate is not calling directly.After completion of data downloading from server, application delegate methods are calling.So, How can i get the immediate Delegate call in my application.
Any help can be appreciate.

Which delegate methods are you implementing? Can you post what you've tried (code-wise) here? Note that once your application has been moved to the background, you can only run short-term tasks to preserve battery life.
This example is provided by Apple:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you.
// stopped or ending the task outright.
[app 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.
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
If you want to perform longer running tasks, you require special permissions, and even then can only perform a certain subset of actions. They are discussed here:
iOS App Programming Guide: App States and Multitasking

Related

Sending Local notifications when the app is in background mode not working

I want to be able to send multiple local notifications throughout the day, this must also work in background mode and it must work on a timer, so every 20 minutes to be exact. The problem I am having is that once in background mode they are not firing because the function used to set the timer will not run.
I have then thought about running the timer in the applicationDidEnterBackground but this would clash with the original timer set in the function.
Is there anyway for this to work basically? Any wisdom would be very grateful.
Try looking this link and perfrom your task in background
- (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;
});
}

Background task in objective c

I have a VOIP application in objective-c
SIP call is working fine when application is in foreground.
My problem is when my app is in background SIP call is not working after 10 minutes after going to background.
I have started a background task in applicationDidEnterBackground
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;
});
In plist file UIBackgroundModes is set to voip
I am in a patience for it.
Please any body help me. Please.
By my understanding the beginBackgroundTaskWithExpirationHandler method is used for executing a Finite-Length Task in the Background. See Here. And there is no way extend that period of 10 mins which I have discovered.
Since your app classifies as a background mode required app, I dont think you will be needing
beginBackgroundTaskWithExpirationHandler.
Read "Implementing a VoIP App" section in the above link.It advocates the use of setKeepAliveTimeout:handler: method.

how can I use UIWebview as always working on background

I would like to set UIWebview always running on background.
UIWebview is not working when iPhone entered 'applicationDidEnterBackground'.
so I want UIWebview to run always on backgroud.
Is it possible?
Every app can continue to execute in the background for roughly 10 minutes before it terminates. Only certain apps can continue to execute in the background, such as audio/gps/bluetooth etc related apps. You can find out more here.
The following code sample is from the app doc and can help you get started -
- (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;
});
}

Iphone: Does application freeze when run into background?

I have an application that upload file to server using NSUrlConnection. It was placed on ViewDidLoad method. It did upload to server while the application is in foreground. Before I call the NSUrlConnection asynchronously, I create temporary file in application directory.
While the files were uploading, I clicked on the iPhone button so that the application will run in the background. First thing I thought the application freeze the upload, but it doesn't. The file still uploading during that time.
Does that mean the application still running normally at the background until the remaining time finished then the application quits?
Once the home button is pressed, every application runs in the background for a certain amount of time(usually few seconds) before it enters suspended mode. If you are required to complete a task(in your case file upload) before entering suspended mode, you can use Task completion API.
Even if you are using task completion API, only one thread of your application is running, but not the whole application. Here is the code recommended by apple:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app 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.
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}

How to override exit action (iphone dev.)?

I need to write some information to file before exiting application. How can I do that?
Supposing your app supports multitasking (and you're on iOS 4 or above):
Override the applicationDidEnterBackground method in your app delegate, and do like the follwing:
- (void)applicationDidEnterBackground:(UIApplication *)application {
bgTask = [ [UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
//do whatever writing you like
if (bgTask != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
}
Implement the following method in your App Delegate:
- (void)applicationWillTerminate:(UIApplication *)application
Start with Preserving the State of Your Application’s User Interface on Apple's Developer site. Read about the methods applicationDidEnterBackground: and it's related methods like applicationWillResignActive, etc. One thing to keep in mind when implementing any of these application delegate methods is that you don't have a lot of time to do stuff.
One way to avoid the race condition of trying to write out some data while the app is being backgrounded or exiting is to write it out when it changes during the normal course of your app running. Then, it's always correct "on disk."