Developer David Nottage proposes a nice solution to keeping your applications to run in the background on iOS. On iOS as well as on Android, it is the system that decides when to terminate an app for very specific reasons (i.e. memory starvation). For this particular reason, the onClose event of the app might not trigger, since the system decides to immediately kill the application. Thus, you should never place important code in this event, since it will not execute and even worse you could cause memory leaks if you try to free the memory in the onClose event and this event is not fired. Suspended applications receive no notification when they are terminated unlike the applications that run in the background and are not in the suspended state. In this case, the system calls applicationWillTerminate before the actual onClose event. In Delphi, in the case of UIApplication, two important methods have been left out:
function beginBackgroundTaskWithExpirationHandler(handler: TBackgroundTaskHandler): UIBackgroundTaskIdentifier; cdecl; procedure endBackgroundTask(identifier: UIBackgroundTaskIdentifier); cdecl;
The demo, wraps the two methods and provides easy and convenient integration. The implementation applies to Delphi 10 Berlin with FireMonkey. Here is a small sample of the demo implementation, just to give an idea and stir your interest to study it in more detail. This method is not related to UIBackgroundModes.
function TBackgroundTaskManager.Start: Boolean; begin FTaskID := SharedApplication.beginBackgroundTaskWithExpirationHandler(DoExpiry); Result := IsValidTask; end; procedure TBackgroundTaskManager.Stop; begin if IsValidTask then SharedApplication.endBackgroundTask(FTaskID); Reset; end;