Overloading a method in FMX.Platform.iOS

会有一股神秘感。 提交于 2019-12-12 02:28:40

问题


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:


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.



来源:https://stackoverflow.com/questions/34023980/overloading-a-method-in-fmx-platform-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!