ios - 如何创建一个 UIBackgroundTask 以在应用程序处于后台时发送数据?

标签 ios delphi firemonkey

我在 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/

相关文章:

delphi - Delphi中如何创建文件夹和txt文件

ios - UIAlertController 中的组或分隔符与新的音乐应用程序一样

delphi - XE6 中的 TOpenFileDialog 访问冲突

android - 修复了 Android 上使用 fmx 字体的 : canvas. 填充文本被 block 背景包围的问题

macos - 在 Mac 上进行 Delphi 开发的建议?

delphi - 使用 FastMM4 时如何从代码中检测

image - TImageViewer中,如何获取用户点击图片的位置?

ios - 如何在 iOS 6.1 上重现网站崩溃

ios - Swift 中的属性和变量有什​​么区别?

ios - 如何快速循环保存在后台