ios - 使用 Delphi XE5 在 UIView 类 [Swizzling] 中开始 Hook

标签 ios objective-c delphi delphi-xe5 swizzling

我正在尝试在全局范围内捕获所有触摸事件。为此,我知道我可以在 UIView 类中挂接触摸事件过程。我有编译的代码。我的钩子(Hook)实现是

procedure touchesBeganDetour(self: id; _cmd: SEL; touches: NSSet; withEvent: UIEvent); cdecl;
begin
  Sleep(1);
end;

然后我尝试用两种不同的方式连接它。第一:

constructor TTouchEventListener_IOS.Create;
var
  FM1, FM2: Pointer
  ViewClass: Pointer;
begin
  inherited;

  ViewClass := objc_getClass('UIView');
  class_addMethod(ViewClass, sel_getUid('touchesBeganDetour:'), @touchesBeganDetour, 'v@:@@');
  FM1 := class_getInstanceMethod(ViewClass, sel_getUid('touchesBegan:withEvent:'));
  FM2 := class_getInstanceMethod(ViewClass, sel_getUid('touchesBeganDetour:'));
  method_exchangeImplementations(FM1, FM2);
end;

这似乎是标准方法。第二个:

constructor TTouchEventListener_IOS.Create;
var
  FM1
  ViewClass: Pointer;
begin
  inherited;

  ViewClass := objc_getClass('UIView');
  FM1 := class_getInstanceMethod(ViewClass, sel_getUid('touchesBegan:withEvent:'));
  method_setImplementation(FM1, @touchesBeganDetour);
end;

据我所知,这也应该有效。我得到了“touchesBegan:withEvent”的实例,并且所有代码都没有错误地执行。但是,当我随后触摸模拟器屏幕时,代码在“Macapi.ObjectiveC.pas”单元中的“DispatchToImportSuper”内部崩溃。我显然做错了什么,但我不知道是什么。如果这可行,就可以在不修改 Delphi 源代码的情况下监听触摸事件。

有人有什么想法吗?

最佳答案

再次回答我自己的问题。问题出在绕行程序声明中。看起来你不能指定原始参数,但你必须使用指针而不是接口(interface)。这可能是由于 objectiveC 和 object pascal 之间的差异。您稍后“包装”并将指针转换为正确的接口(interface)。

procedure touchesBeganDetour(self: id; _cmd: SEL; touches: Pointer; withEvent: Pointer); cdecl;
begin
  DoNotifyTouchEvent(TNSSet.Wrap(touches), TUIEvent.Wrap(withEvent), teDown);
end;

关于ios - 使用 Delphi XE5 在 UIView 类 [Swizzling] 中开始 Hook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21548207/

相关文章:

multithreading - Windows Server 2003上的多线程应用程序中的访问冲突

ios - Swift:将 View 从堆栈 View 带到前面

iphone - 带有可点击、可排序标题的 UITableView

IOS如何检查数组的值是否包含某个值并将其显示出来

objective-c - 检索应用程序的 DockTile( View )

sql - 如何从 ADO 查询中获取记录数?

ios - UITextField 通知

ios - Json转换问题 : invalid characters found

ios - UIScrollView 中的多个 AVPlayer - 只显示了其中的 16 个

delphi - 如何更改控件内的鼠标光标?