36

first of all, I know there is only support for voip, audio and location apps to run in background and that they will run just while the audio is been played or while using location services, etc.

What I want to know is if there is a way to keep my app running on background fully operational, doesn't matter the impact on battery's life.

That way the user of my app can select from settings to keep alive the app whenever he wants and just for the amount of time he wish. e.g if he is waiting for something that requires the app to be running, after receiving the messages he can turn off the keep alive functionality.

I don't know if this is possible but I had read some post that say so but unfortunately they didn't say how to =(

UPDATE: In this tutorial, I found that Acrobits has two apps on the Apple Store that "can force the application to stay alive and awake in the background". So there is a way to do this?

Gilles 'SO- stop being evil'
  • 92,660
  • 35
  • 189
  • 229
Paul N
  • 1,881
  • 1
  • 22
  • 32

6 Answers6

32

From ioS 7 onwards, there are newer ways for apps to run in background. Apple now recognizes that apps have to constantly download and process data constantly.

Here is the new list of all the apps which can run in background.

  1. Apps that play audible content to the user while in the background, such as a music player app
  2. Apps that record audio content while in the background.
  3. Apps that keep users informed of their location at all times, such as a navigation app
  4. Apps that support Voice over Internet Protocol (VoIP)
  5. Apps that need to download and process new content regularly
  6. Apps that receive regular updates from external accessories

You can declare app's supported background tasks in Info.plist using X Code 5+. For eg. adding UIBackgroundModes key to your app’s Info.plist file and adding a value of 'fetch' to the array allows your app to regularly download and processes small amounts of content from the network. You can do the same in the 'capabilities' tab of Application properties in XCode 5 (attaching a snapshot)

Capabilities tab in XCode 5 You can find more about this in Apple documentation

Pradeep Mahdevu
  • 7,517
  • 2
  • 29
  • 28
  • I have a similar situation where I am connecting a BLE device to my IOS app. The device is emitting data constantly but the app pauses the listener as soon as the app goes in the background and resumes it again. What I want to do is to continue the listener stream even if the app goes in the background. So, I've got two questions, 1.) Is background fetch a possible solution for that? 2.) If Yes, i was reading you can trigger background fetch after a certain time interval has passed, but in my case that interval is 0.00 nanoseconds (like I don't want to have any gap in the incoming stream) – Siddharth Choudhary May 03 '21 at 16:05
  • And I'll stop the service in 5-10 minutes – Siddharth Choudhary May 03 '21 at 16:17
21

You can perform tasks for a limited time after your application is directed to go to the background, but only for the duration provided. Running for longer than this will cause your application to be terminated. See the "Completing a Long-Running Task in the Background" section of the iOS Application Programming Guide for how to go about this.

Others have piggybacked on playing audio in the background as a means of staying alive as a background process, but Apple will only accept such an application if the audio playback is a legitimate function. Item 2.16 on Apple's published review guidelines states:

Multitasking apps may only use background services for their intended purposes: VoIP, audio playback, location, task completion, local notifications, etc

Brad Larson
  • 168,330
  • 45
  • 388
  • 563
  • I know I can perform a taks for 10 min max on background, but I will like to know if I can force the app to keep running on background indefinitely. Like the backgrounder app on jailbreaked iPhones, but that will definitely wont be approved by Apple. The playing a no sound audio in the background has a lot of chances of been approved but I really dont like that approach. Thanks for your help anyway Brad. – Paul N Sep 21 '10 at 21:01
  • @Paul N - Actually, playing silent audio will get you rejected. I'm referring to Pastebot's implementation of playing audio to stay in the background: http://tapbots.com/blog/pastebot/pastebot-music-in-background . They needed to play an audible music track in order to be accepted. – Brad Larson Sep 21 '10 at 21:26
  • Hey Brad, the Pastebot idea about letting the user select a no audio or whatever audio from their music library is really a good idea. Anyway I still think that there should be a better workaround out there but at least is something. I wont mark this question as answered because I hope somebody else may come with a better solution. But sure this is the closest right now. Thanks. – Paul N Sep 21 '10 at 21:41
  • There wasn't a better workaround so I marked your answer as the most helpful for me on that time. Thanks – Paul N Apr 07 '11 at 20:55
  • iOS 7 changed running stuff in background kinda heaviely :) – Lukas Jun 12 '14 at 18:26
  • link is obselete – iosMentalist Nov 14 '19 at 08:36
8

If any background task runs more than 10 minutes,then the task will be suspended and code block specified with beginBackgroundTaskWithExpirationHandler is called to clean up the task. background remaining time can be checked with [[UIApplication sharedApplication] backgroundTimeRemaining]. Initially when the App is in foreground backgroundTimeRemaining is set to bigger value. When the app goes to background, you can see backgroundTimeRemaining value decreases from 599.XXX ( 1o minutes). once the backgroundTimeRemaining becomes ZERO, the background task will be suspended.

        //1)Creating iOS Background Task
        __block UIBackgroundTaskIdentifier background_task;
        background_task = [application beginBackgroundTaskWithExpirationHandler:^ {

               //This code block is execute when the application’s 
               //remaining background time reaches ZERO.
          }];


        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //### background task starts

            //#### background task ends
        });

        //2)Making background task Asynchronous
        if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
        {
            NSLog(@"Multitasking Supported");

            __block UIBackgroundTaskIdentifier background_task;
            background_task = [application beginBackgroundTaskWithExpirationHandler:^ {

                //Clean up code. Tell the system that we are done.
                [application endBackgroundTask: background_task];
                background_task = UIBackgroundTaskInvalid;
            }];


        **//Putting All together**
            //To make the code block asynchronous
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                //### background task starts
                NSLog(@"Running in the background\n");
                while(TRUE)
                {
                    NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
                    [NSThread sleepForTimeInterval:1]; //wait for 1 sec
                }
                //#### background task ends

                //Clean up code. Tell the system that we are done.
                [application endBackgroundTask: background_task];
                background_task = UIBackgroundTaskInvalid; 
            });
        }
        else
        {
            NSLog(@"Multitasking Not Supported");
        }
Sabareesh
  • 3,415
  • 2
  • 19
  • 41
3

For running on stock iOS devices, make your app an audio player/recorder or a VOIP app, a legitimate one for submitting to the App store, or a fake one if only for your own use.

Even this won't make an app "fully operational" whatever that is, but restricted to limited APIs.

hotpaw2
  • 68,014
  • 12
  • 81
  • 143
  • Apple doesn't perform a lot of testing to applications, I can grant you that. Indeed I know I can play a no sound audio every 10 seconds to keep my app running on background. But I just wanted to know if there was another way to keep my app running on background. Thanks anyway. – Paul N Sep 21 '10 at 20:57
  • Nice answer, but ... is this a normal performance ? And if apple might mark it as a virus, unstable or malicious app ? – F8ER Aug 22 '16 at 06:35
2

Depends what it does. If your app takes up too much memory, or makes calls to functions/classes it shouldn't, SpringBoard may terminate it. However, it will most likely be rejected by Apple, as it does not follow their 7 background uses.

jrtc27
  • 8,386
  • 3
  • 33
  • 68
1

May be the link will Help bcz u might have to implement the code in Appdelegate in app run in background method .. Also consult the developer.apple.com site for application class Here is link for runing app in background

swiftBoy
  • 33,793
  • 26
  • 129
  • 124
Zeeshan
  • 3,886
  • 25
  • 32