gpt4 book ai didi

ios - 从 delphi 调用 objective-c 代码块

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:05:34 29 4
gpt4 key购买 nike

我正在尝试在我的 firemonkey 应用程序中进行后台获取。

到目前为止,我的 perfromFetchWithCompletionHandler 被调用并下载了新信息。

当我完成获取并需要调用 completionHandler 代码块时,问题就来了,应用程序挂起并且我没有收到任何异常(我至少可以阅读)

设置:

TBackgroundFetchResultHandlerC = procedure ( AResult : NSUInteger ); cdecl;
..
..

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

objc_msgSend((UIApp as ILocalObject).GetObjectId,
sel_getUid('setMinimumBackgroundFetchInterval:'),
Interval);
class_addMethod( objc_getClass('DelphiAppDelegate') ,
sel_getUid('application:performFetchWithCompletionHandler:'),
@performFetchWithCompletionHandler,
'v@:@?'
);
..
..

procedure performFetchWithCompletionHandler(self : id; _cmd : SEL; application: PUIApplication; handler : TBackgroundFetchResultHandlerC );
var t : TThread;
begin
NSLog3('performFetchWithCompletionHandler:begin');
Handler(0); <<--Stops here
//Next line of code never gets called.
NSLog3(Format('performFetchWithCompletionHandler:done, done',[ FetchResult ]) );
end;

performFetchWithCompletionHandler man page

我已经尝试过函数指针类型的不同声明,但由于它不是一个函数指针,我想这就是它不起作用的原因。

有什么想法吗?

谢谢罗伯特

最佳答案

我找到了一个有效的解决方案:imp_implementationWithBlock

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

//将处理程序更改为类型 id(指针)

const
libobjc = '/usr/lib/libobjc.dylib';

{$IF NOT DECLARED(_PU)}
const
{$IFDEF UNDERSCOREIMPORTNAME}
_PU = '_';
{$ELSE}
_PU = '';
{$ENDIF}
{$EXTERNALSYM _PU}
{$ENDIF}

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';

//添加了对 libobjc 中 imp_implementationWithBlock 和 imp_removeBlock 的引用

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

//声明类型 IMP 作为 c 函数对应于 imp_implementationWithBlock 在这种特殊情况下返回的函数指针,带有一个 NSUInteger 参数。

procedure performFetchWithCompletionHandler(self : id; _cmd : SEL; application: PUIApplication; handler : id );
var
ahandlerimp : IMP;
begin
.... //Code to perform fetch
ahandlerimp := imp_implementationWithBlock( handler ); //Create c function for block
ahandlerimp(self,_cmd,FetchResult ); //Call c function, _cmd is ignored
imp_removeBlock(ahandlerimp); //Remove the c function created two lines up
end;

关于ios - 从 delphi 调用 objective-c 代码块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23130639/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com