http://stackoverflow.com/questions/32348081/delphi-firemonkey-ios-background-processing

I finally got it to work under delphi 10 Seattle (you need this version, i have tried XE7 and doesn't work, I haven't tried xe8). Here are the steps:

Project > Options > Version Info. Check UIBackgroundModes you need (I used Fetch)

1。设置setMinimumBackgroundFetchInterval时间间隔

2.实现performFetchWithCompletionHandler方法

unit uBackgroundiOS;

interface

uses fmx.platform.iOS,iOSapi.CocoaTypes, Macapi.ObjCRuntime, Macapi.ObjectiveC,
     iOSapi.UIKit;

const
  UIBackgroundFetchResultNewData:NSUInteger = 0;
  UIBackgroundFetchResultNoData:NSUInteger = 1;
  UIBackgroundFetchResultFailed:NSUInteger = 2;

  UIApplicationBackgroundFetchIntervalMinimum:NSTimeInterval = 0;
  UIApplicationBackgroundFetchIntervalNever:NSTimeInterval = -1;

type

  // copied from fmx.platform.iOS as it's on private declaration
  id = Pointer;
  SEL = Pointer;
  PUIApplication = Pointer;

  IMP = function( self : id; cmd : SEL; Param1 : NSUInteger ) : id; cdecl;

  function imp_implementationWithBlock( block :id ) : IMP; cdecl; external libobjc name  _PU + 'imp_implementationWithBlock';
  function imp_removeBlock( anImp : IMP ) : integer; cdecl; external libobjc name _PU + 'imp_removeBlock';

  procedure performFetchWithCompletionHandler(self : id; _cmd : SEL; application:    PUIApplication; handler : id );

  procedure initializeBackgroundFetch;

  function objc_msgSend(theReceiver: Pointer; theSelector: Pointer): Pointer; cdecl; varargs;
  external libobjc name _PU + 'objc_msgSend';

  //to test if procedure is called in background
  var fecth_string_test: string;

implementation

procedure performFetchWithCompletionHandler(self : id; _cmd : SEL; application: PUIApplication; handler : id );
var
  ahandlerimp: IMP;
begin
  //Code to perform fetch HERE!!!!
  fecth_string_test := 'entered background code!!';
  surl:='www.cnblog.com/abc/123.html';
  ahandlerimp := imp_implementationWithBlock( handler ); //Create c function for block
  ahandlerimp(self,_cmd, UIBackgroundFetchResultNewData); //Call c function, _cmd is ignored 接收到的新数据
  imp_removeBlock(ahandlerimp); //Remove the c function created two lines up


end; procedure initializeBackgroundFetch; Var UIApp: UIApplication; begin UIApp := TUIApplication.Wrap(TUIApplication.OCClass.sharedApplication); objc_msgSend((UIApp as ILocalObject).GetObjectId, sel_getUid('setMinimumBackgroundFetchInterval:'), UIApplicationBackgroundFetchIntervalMinimum); class_addMethod(objc_getClass('DelphiAppDelegate') , sel_getUid('application:performFetchWithCompletionHandler:'), @performFetchWithCompletionHandler, 'v@:@?'); end; end.

 

也可以在didFinishLaunchingWithOptions事件里设置UIApplicationBackgroundFetchIntervalMinimum时间

 执行完了要调用completionHandler告知系统。

[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

performFetchWithCompletionHandler是回调函数,可以在里边写个通知!
http://www.cnblogs.com/skinWalkers/p/3730118.html

讲的好!
http://www.cnblogs.com/zanglitao/p/4113283.html

performFetchWithCompletionHandler Declaration

Swift

         optional func application(_ application: UIApplication,
performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)

Objective-C

- (void)application:(UIApplication *)application
performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler

Parameters

application

Your singleton app object.

completionHandler

The block to execute when the download operation is complete. When calling this block, pass in the fetch result value that best describes the results of your download operation. You must call this handler and should do so as soon as possible. For a list of possible values, see the UIBackgroundFetchResult type. 

 

posted on 2016-01-10 15:27  lypzxy  阅读(759)  评论(0编辑  收藏  举报