I m under delphi berlin. My app is in background listening for location updates. When their is a location update in background i need to send the data to a server, but as i read on the internet i need to do this inside a UIBackgroundTask
else the app will enter again in the background before the data is actually send. Any idea how to do this under delphi ?
Here's some code that should get you started:
uses
Macapi.ObjectiveC, iOSapi.Foundation, iOSapi.CocoaTypes, iOSapi.UIKit;
const
UIKitFwk: string = '/System/Library/Frameworks/UIKit.framework/UIKit';
type
TBackgroundTaskHandler = procedure of object;
// Fills in methods missing in Delphi
UIApplication = interface(UIResponder)
['{8237272B-1EA5-4D77-AC35-58FB22569953}']
function beginBackgroundTaskWithExpirationHandler(handler: TBackgroundTaskHandler): UIBackgroundTaskIdentifier; cdecl;
procedure endBackgroundTask(identifier: UIBackgroundTaskIdentifier); cdecl;
end;
TUIApplication = class(TOCGenericImport<UIApplicationClass, UIApplication>) end;
function SharedApplication: UIApplication;
begin
Result := TUIApplication.Wrap(TUIApplication.OCClass.sharedApplication);
end;
function UIBackgroundTaskInvalid: UIBackgroundTaskIdentifier;
begin
Result := CocoaIntegerConst(UIKitFwk, 'UIBackgroundTaskInvalid');
end;
When your app receives a location update, you can make a call like:
TaskID := SharedApplication.beginBackgroundTaskWithExpirationHandler(DoExpiry);
Where DoExpiry is a method you define that handles when the OS indicates that your task time has expired. Check the result against UIBackgroundTaskInvalid.
When your task is finished, call:
SharedApplication.endBackgroundTask(TaskID);