Overloading a method in FMX.Platform.iOS

2019-09-01 10:35发布

within FMX.Platform.iOS this method is added to the App delegate

class_addMethod(appDelegateClass, sel_getUid('application:didReceiveLocalNotification:'),    
@applicationDidReceiveLocalNotification, 'v@:@@');

And its procedure is :

procedure applicationDidReceiveLocalNotification(self: id; _cmd: SEL; application: PUIApplication;
notification: Pointer); cdecl;

begin
  PlatformCocoa.FAppDelegate.application(TUIApplication.Wrap(application), TUILocalNotification.Wrap(notification));
end;

I want to overload this method in my main unit, but it only works if I remove the declaration of it from FMX.Platform.iOS

In my main unit I add the Method as follows:

class_addMethod(objc_getClass('DelphiAppDelegate'), sel_getUid('application:didReceiveLocalNotification:'),
@applicationDidReceiveLocalNotification, 'v@:@@');

And my procedure:

procedure applicationDidReceiveLocalNotification(self: id; _cmd: SEL;
application: PUIApplication; notification: Pointer);
begin
    ShowMessage('it works');
end;

I want to try and Keep out of the FMX.Platform.iOS source as much as I can, is there a way to tell the compiler to use my applicationDidReceiveLocalNotification and not the one in the source?

1条回答
走好不送
2楼-- · 2019-09-01 11:00

Since Embarcadero's method is added using Objective-C's class_addMethod(), try using Objective-C's class_replaceMethod() function to replace it with your own implementation. There are tons of online tutorials that explain how to use method swizzling in iOS to add/override methods. Please search around. You will not find any Delphi examples, though, so you will have to do some translating.

查看更多
登录 后发表回答