Force app to close and run in background - iphone

we have an app that has a specific purpose where an exit is required. After the exit a process needs to run in the background for a certain amount of time or until finished. We just need to know how to programmatically force the app to enter the background where processes can continue running. Any help on this would be great! Thanks in advance!
UPDATE: We have confirmed that there does not seem to be a programmatic way to force the app to quit / enter background and continue running background tasks. You can force the the app to exit using exit(0); but this kills the app all together. However, the bulk of this question was concerning running tasks in the background. We have found a solution that allows our app to begin processing data and handling tasks that a user has setup to be processed. Here is the code required. This needs to be added to the app delegate and multitasking is required on the device / IOS.
- (void)applicationDidEnterBackground:(UIApplication *)app{
// Check that IOS supports multitasking
if ([[UIDevice currentDevice] respondsToSelector:#selector(isMultitaskingSupported)]){
// Check that the device supports multitasking
if ([[UIDevice currentDevice] isMultitaskingSupported]) {
// Custom setting to allow users the freedom to enable or disable background tasks
BOOL enabled = [[NSUserDefaults standardUserDefaults] boolForKey:#"backgroundTasksEnabled_key"];
if ( enabled ){
//Get the shared application instance
backGroundApp = [UIApplication sharedApplication];
background_task = [backGroundApp beginBackgroundTaskWithExpirationHandler: ^ {
[backGroundApp endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
}];
// Run in background
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(#"\n\nProcess background tasks!\n\n");
// Do your work here
});
}
}
}
}

You can force an iOS app into the background by sending a registered URL to another app to handle, such as a web site URL to Safari.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: myWebsiteURL ]];
Many many apps that call Safari to handle URLs are approved by Apple.
To get any time in the background, an app has to be appropriately configured using one of the allowed background modes (audio, location or VOIP); or the app can request a few minutes of extra time in the background before being suspended by calling the beginBackgroundTaskWithExpirationHandler method

You can't have a process running (doing work) in the background in iOS, you get a few seconds when the app quits to do any clean up and that's it!

You cannot force an application into the background, I'm fairly sure that Apple's guidelines prohibit you from doing that. What could your app possibly be doing that it can only do in the background and not in the foreground?

Related

Network disconnect issue when iOS screen locks

In iOS 5 When Application Enter background wi-fi connection is lost.
But I want to use wi-fi connection for the next 4-5 minutes before the device sleeps as some tasks can be performed within 4-5 minutes of application enter background.
I think this can be accomplished by using beginBackgroundTaskWithExpirationHandler:, but i am not able to solve the problem
just disable iPhone to go to sleep mode
-(void) sleepModeDisable{
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}
call this function every 10 second, this might help u
The way I handle this is to use beginBackgroundTaskWithExpirationHandler for every network request I'm sending.
This way I make sure that all my networking will completed even if my app moved to background.
I'm usually using one singleton object to handle all network request, so before the request is sent I call
- (void)startBackgroundTask
{
// ask for extra time if this is called when app go to suspended
UIApplication *application = [UIApplication sharedApplication];
_bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you.
// stopped or ending the task outright.
[application endBackgroundTask:_bgTask];
_bgTask = UIBackgroundTaskInvalid;
}];
}
And after I get a response (success/failure) or if I canceled the request, I call
- (void)stopBackgroudTask
{
UIApplication *app = [UIApplication sharedApplication];
if (_bgTask != UIBackgroundTaskInvalid) {
[app endBackgroundTask:_bgTask];
_bgTask = UIBackgroundTaskInvalid;
}
}
* Don't forget to define UIBackgroundTaskIdentifier *_bgTask;
Also if you are planning to make a massive use of Wi-Fi you should set the Application uses Wi-Fi key in your plist file to YES, otherwise your Wi-Fi will be shut done after 30 minutes even if your app is running.
No rocket science here, this is intended behavior in iOS that to save battery the Wi-Fi shuts off when phone is locked UNLESS you tell iOS that your app needs a persistant Wi-Fi, then it wont close it for you when your app is running.
For that just add UIRequiresPersistentWiFi to your info.plist and mark it YES
Documentation

How to know if a task is executing in background

In my app I'm downloading lots of images on a method.
I'm using a
downloadTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:downloadTask];
downloadTask = UIBackgroundTaskInvalid;
}];
This is working fine, if I press the home or sleep button, the images continue downloading.
I'm showing the progress in a UIProgressView inside an UIAlertView, and when the percent is 100% the alertView is dissmised and I change the viewController to other where I show the donwloaded images.
But I only want this to happen if the app is really active at the moment the download finish.
I have been looking at the app state and while it's downloading with the screen off.
[UIApplication sharedApplication].applicationState
the state is UIApplicationStateActive during all the donwload
How can I can know if the downloading is happening with the screen off or on?
EDITED AFTER ACCEPTING THE ANSWER:
I just discovered, if I tap the home button, the app enters in UIApplicationStateBackground, if I tap the wake/sleep it enters in UIApplicationStateInactive
Following the approach of the correct answer, my app contines donwloading in both cases.
The screen is off in two states (apart from when the app has not been even opened):
suspended : in this case you don't have to worry because the download won't procede until the app gets active again; It will enter this state on
background : it's in this state for a limited amount of time before going in suspend, and the screen is already off in this moment. Here you may want to check then whether to do all the things you said or not, because in this state code can be still executed. In this state the app status is UIApplicationStateBackground, so you could just perform a check like this:
You probably want to check whether the app is in background execution in order to achieve the result. Just like this:
if([[UIApplication sharedApplication] applicationState] != UIApplicationStateBackground) {
// Do stuff
}
If it's in background, so the screen is off.
UPDATE: after few test, what I figured out is that the behaviour you are expieriencing is probably due to the execution of the download on the main thread.
You should send the download on (for instance) the global queue. This way the application will enter the background state as expected:
....
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.bti];
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self doBackgroundStuff];
});
....
This way, if the app is put on background while the download is in progress, the application state will turn into UIApplicationStateBackground, and then you can check it as I wrote initially. If you are doing UI updates during the progress remember to send them back to the main thread (because the download is now on a different one).
You can check whether your app is running in the background or not by setting a flag in the designated application delegate methodsapplicationDidEnterBackground: and applicationWillEnterForeground:. Example:
- (void)applicationDidEnterBackground:(UIApplication *)application
_applicationRunsInForeground = NO;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
_applicationRunsInForeground = YES;
}
If you don't want to have this _applicationRunsInForeground flag inside your application delegate, you could observe the delegate's NSNotifications in your viewcontroller class instead (UIApplicationWillEnterForegroundNotification and UIApplicationDidEnterBackgroundNotification).

iPhone app is automatically being logged off/killed after approximately every 20 minutes

PLATFORM AND VERSION iOS
iPhone 4s, iOS version 5.1.1
DESCRIPTION OF PROBLEM
Whenever my application is placed in the background it is automatically being logged off / killed after approximately every 20 minutes. In our application we have the ability to upload data if the app is moved to background. If I comment out this section, the application is no longer killed.
The application gets logged off (killed by the operating system) even if there is no activity or no user intervention and its just sitting in the background.
STEPS TO REPRODUCE
Please find below the code snippet which is uploading the attachments
in the background.
-(void) endTaskWidthIdentifier:(NSNumber *)paramIdentifier{
UIBackgroundTaskIdentifier identifier = [paramIdentifier integerValue];
[[UIApplication sharedApplication] endBackgroundTask:identifier]; }
- (void)applicationDidEnterBackground:(UIApplication *)application {
backgroundTaskIdentifier = [application
beginBackgroundTaskWithExpirationHandler:^{
NSNumber *backgroundTask = [NSNumber
numberWithInteger:backgroundTaskIdentifier];
[self
performSelectorOnMainThread:#selector(endTaskWidthIdentifier:)
withObject:backgroundTask waitUntilDone:YES];
backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}];
}
Once this code is executed, the application logs out after 15-20 minutes when kept in the background with no user intervention.
If I comment this code, application remains in the background and does not exits and remains in the suspended mode.
Question:
What is wrong with this code. What is this code doing that it should not?

background task running on iphone

I have some test code which i am using to keep my app to stay running in the background state, which works quite well on the iOS 5.1 simulator but is not behaving that way on the actual device.
Now i already know the requirements i have to follow,
so i have set "background modes" to voip and location.
In my delegate's applicationDidEnterBackground method i am calling the following function
which i call "doBackgroundActivity( )"
to request time for application to complete some long running task in background :
-(void) threadedMethod{
while(true){
NSLog(#"looping");
[self showLocalNotification:#"This notification will come every 2 min. if the app is running in background. Close it!"];
[NSThread sleepForTimeInterval: (5)]; // 60 sec = 1 min
}
}
-(void) doBackgroundActivity{
self->_backgroundTask = [ [UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask: self->_backgroundTask];
self->_backgroundTask = UIBackgroundTaskInvalid;
}
];
[self threadedMethod];
[[UIApplication sharedApplication] endBackgroundTask:self->_backgroundTask];
self->_backgroundTask = UIBackgroundTaskInvalid;
}
As you see, the doBackgroundAcitivity() just calls the threadedMethod() and all that does is run an infinite loop which sends a local notification every 5 seconds or so.
Now in the simulator when i run this, and minimize the app, i see a notification every 5 seconds or so. The application keeps on running in the background even when i run other applications i see my app sending notifications.
But this is not happening on the device. It looks like that the OS kills the app just after the first notification is send and i dont see any more subsequent notifications which i expect to see later?
Is there something else i have to do to keep the application running in background ?
If all you are looking to do is present a notification to the user while your app is closed you should use UILocalNotifications.
You can set when the notification should fire, as well as whether or not it should repeat, and the amount of time between repeats.
As a side note, i wouldn't recommend using sleep() in a loop to control when code is executed like that because it stops the thread from doing anything at all, and is bad for any other tasks that would like to use that thread. Instead you should use NSTimers, and other ways of controlling the time at which code is executed.

Keep iphone active while running program

how to set iPhone device to stay active ( to not lock ) while my app is running ?
Any idea
I'm not sure if this prevents the device from locking, but you can prevent the screen from dimming with the UIApplication's idleTimerDisabled property:
[UIApplication sharedApplication].idleTimerDisabled = YES;
From the documentation:
Important: You should set this property only if necessary and should be sure to reset it to NO when the need no longer exists. Most applications should let the system turn off the screen when the idle timer elapses. This includes audio applications. With appropriate use of Audio Session Services, playback and recording proceed uninterrupted when the screen turns off. The only applications that should disable the idle timer are mapping applications, games, or similar programs with sporadic user interaction.
This code will prevent your iPhone from going to sleep while your app is running
// avoid sleeping when this application is running
UIApplication *application = [UIApplication sharedApplication];
application.idleTimerDisabled = YES;
// Or simpler
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
If you landed here looking for an answer in Swift, it's this:
UIApplication.sharedApplication().idleTimerDisabled = true
for Swift 3
UIApplication.shared.isIdleTimerDisabled = true
The warning in this comment still applies.