NOTE: This article is about allowing your iOS app to run when it goes into the background (i.e. another app becomes active) for a short period (up to 3 minutes on iOS 9, at least). It does not relate to having the UIBackgroundModes option, an example of which is here.

As some of you will already know, some methods on iOS classes which have been “imported” into Delphi, have been left out, for whatever reason that might be, and some are quite unfortunate, because it means having to work around it, as per the example I linked to, above.

This is the case for UIApplication, where two methods related to enabling an iOS app to run when the app is in the background, have been left out; namely:


function beginBackgroundTaskWithExpirationHandler(handler: TBackgroundTaskHandler): UIBackgroundTaskIdentifier; cdecl;
procedure endBackgroundTask(identifier: UIBackgroundTaskIdentifier); cdecl;

I knew of this omission a while ago, and a recent post on Google+ prompted me to look into it in more depth. The two methods seemed easy enough to use, so I put together a class that wraps the 2 calls and makes using them a bit more convenient. The result is in the demo that follows at the end of this article. Rather than going too deep into explanations, I’ll allow the reader to have a look at it, and I’ll field questions if necessary 🙂