Ios: Keep an App Running Like a Service

iOS: Keep an app running like a service

Basically, there is no such thing as a service type app or functionality in iOS.
Even the "background" apps (UIBackgroundMode) cannot run entirely free and without restrictions like a service or daemon etc. on other OSs can.

Here's the situation regarding background execution and notifications and timers etc.

1) An app cannot execute in the background unless:

a) it requests extra time from the OS to do so. This is done using beginBackgroundTaskWithExpirationHandler. It is not specified (intentionally) by Apple how long this extra time is, however in practice it is around 10 minutes.

b) an app has a background mode, the modes are VoIP, audio, location, newsstand. Even if it has one of these types an app cannot execute without restrictions. The rest of this discussion assumes the app does not have a background mode. If you try to use one of these background modes to enable your app to be capable of running in the background but your app does not make legitimate use of the specific functionality then your app will be rejected upon app store submission (i.e. to have a UIBackgroundMode it MUST be either: a VoIP app, NEED to have continual location updates, the ability to play audio in the background continuously is a fundamental feature, or be a newsstand app).

2) When an app is suspended it cannot do ANYTHING to rouse itself directly. It cannot previously have scheduled an NSTimer, it cannot make use of something like performSelector:afterDelay. etc.

The ONLY way the app can become active again is if the USER does something to make it active. The user can do this from via of the following:

a) Launch the app directly from its icon

b) Launch the app in response to a local notification that was previously scheduled by the app while it was active.

c) Launch the app in response to a remote notification sent by a server.

d) A few others: such as URL launching if the app is registered to deal with launching via a URL; or if it's registered to be capable of dealing with a certain type of content.

If an app is in the foreground when a local/remote notification fires then the app receives it directly.

If the app is not currently in the foreground when a local/remote notification fires then the app DOES NOT receive it. There is no code that is executed when the notification fires!

Only IF the user selects the notification will the app become active and it can execute.

Note that the user can disable notifications, either for the entire device or just for a specific application, in which case the user will never see them. If the device is turned off when a notification is due to fire then it is lost.

UPDATE FOR IOS 7

1) there are some new background modes such as background fetch (you still can't be roused by the OS in a deterministic fashion however)

2) there's now a background push notification

3) beginBackgroundTaskWithExpirationHandler time has reduced from 10 minutes to about 3.

How to keep an iPhone app running on background fully operational

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

iOS: Keep application running in background

Yes, no need to jailbreak. Check out the "Implementing long-running background tasks" section of this doc from Apple.

From Apple's doc:
Declaring Your App’s Supported Background Tasks

Support for some types of background execution must be declared in advance by the app that uses them. An app declares support for a service using its Info.plist file. Add the UIBackgroundModes key to your Info.plist file and set its value to an array containing one or more of the following strings: (see Apple's doc from link mentioned above.)

How to get your iOS app to run in the background for longer than 3 minutes?

We haven’t had the old 10 minute window for years so that is simply no longer applicable. That was reduced to 3 minutes in OS 7, and further reduced to 30 seconds in iOS 13. (There is a new concept of background tasks, for running tasks longer than 30 seconds, but the the OS will run these at its own discretion, e.g. at night, when the user is charging their device, so that’s not really applicable here).

The short answer to your question is that you cannot submit apps to the store that run in the background indefinitely unless it’s one of a very limited list of permitted background modes.

If you can go to the “Signing & Capabilities” tab of your target and add “Background Modes” and you can see the list of alternatives:

Sample Image

So, with the caveat that you cannot do precisely what you asked, here are a few observations:

  1. The “External accessory communication” background mode (see ExternalAccessory) seems promising, but that’s for bluetooth or wired connections only. So unless you can refactor/reengineer how your devices communicates, that is likely not a viable solution.

  2. There’s an interesting question of whether just keeping the app running in the foreground might be helpful (e.g. judicious use of idleTimerDisabled). I know this solves a different problem (and should be used only where essential), but perhaps it’s option for you.

  3. If your external device is Internet-enabled and you want it to inform the app of some event, perhaps you can have it communicate its intent to some web service that then triggers an APNS push notification that your device can receive.

Bottom line, you can’t do what you ask, but if you edit your question with more details about the nature of the device and what your iOS app wants to do with that device, we might be able to provide better counsel. But in answer to the general question “can I just keep app running perpetually in the background”, the answer is “no.”

Keep an iOS app running in foreground

You might want to look into beginBackgroundTaskWithExpirationHandler, which lets your app request additional processing time to complete tasks when your app is backgrounded.

That said, what you're looking to do isn't advisable. You should make sure your app can recover from your three minute task in case of unexpected termination or failure. For example, your app could crash unexpectedly or terminate due to low memory - in these cases, you won't get any callbacks to applicationWillTerminate or applicationDidEnterBackground.

So my recommendation would be to look at using iOS's background task support to have your save continue in the background if the user leaves the app, but also ensure that in the event your app be terminated by the system without warning your task is recoverable / can safely be rewound.

Keep running the iOS app on background, collecting data from accelerator and send it to server

iOS will allow you 3 minutes of execution in background. 

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

If you want to keep running there are various "workarounds" but not a stable solution.You can initiate background tasks and then manage them

 -(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];
}];

}


Related Topics



Leave a reply



Submit