我在 delphi berlin 下。我的应用程序在后台监听位置更新。当他们在后台进行位置更新时,我需要将数据发送到服务器,但正如我在互联网上阅读的那样,我需要在 UIBackgroundTask
中执行此操作,否则应用程序将在后台再次进入数据实际发送。知道如何在 delphi 下执行此操作吗?
最佳答案
下面是一些可以帮助您入门的代码:
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;
当您的应用收到位置更新时,您可以调用电话:
TaskID := SharedApplication.beginBackgroundTaskWithExpirationHandler(DoExpiry);
其中 DoExpiry 是您定义的一种方法,用于在操作系统指示您的任务时间已过期时进行处理。根据 UIBackgroundTaskInvalid 检查结果。
当你的任务完成后,调用:
SharedApplication.endBackgroundTask(TaskID);
关于ios - 如何创建一个 UIBackgroundTask 以在应用程序处于后台时发送数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45154421/