IOS上监听用户抓屏的事件

  1. unit CMain;
  2.  
  3. interface
  4.  
  5. uses
  6. System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  7. FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, Macapi.ObjectiveC,
  8. iOSapi.Foundation, System.TypInfo,iOSapi.UIKit,
  9. FMX.Controls.Presentation, FMX.StdCtrls;
  10.  
  11. type
  12. TShotListener=class;
  13. TFormMain = class(TForm)
  14. lblMsg: TLabel;
  15. procedure FormCreate(Sender: TObject);
  16. procedure FormDestroy(Sender: TObject);
  17. private
  18. { Private declarations }
  19. FListener:TShotListener;
  20. public
  21. { Public declarations }
  22. end;
  23.  
  24. IScreenShoted = interface(NSObject)
  25. ['{72E80282-B503-4012-96A2-85540EF061C9}']
  26. procedure OnShot; cdecl;
  27. end;
  28.  
  29. TShotListener=class(TOCLocal)
  30. protected
  31. { TOCLocal }
  32. function GetObjectiveCClass: PTypeInfo; override;
  33. public
  34. procedure OnShot();cdecl;
  35. end;
  36.  
  37. var
  38. FormMain: TFormMain;
  39.  
  40. implementation
  41. uses Macapi.Helpers, Macapi.ObjCRuntime;
  42.  
  43. {$R *.fmx}
  44.  
  45. procedure TFormMain.FormCreate(Sender: TObject);
  46. begin
  47. //
  48. FListener:=TShotListener.Create;
  49. TNSNotificationCenter.Wrap(TNSNotificationCenter.OCClass.defaultCenter).addObserver(FListener.GetObjectID,
  50. sel_getUid('OnShot'), StringToID(NSStrToStr(UIApplicationUserDidTakeScreenshotNotification)), nil);
  51. end;
  52.  
  53. { TShotListener }
  54.  
  55. function TShotListener.GetObjectiveCClass: PTypeInfo;
  56. begin
  57. Result := TypeInfo(IScreenShoted);
  58. end;
  59.  
  60. procedure TShotListener.OnShot;
  61. begin
  62. //
  63. FormMain.lblMsg.Text:='Screen Shotted';
  64. end;
  65.  
  66. procedure TFormMain.FormDestroy(Sender: TObject);
  67. begin
  68. TNSNotificationCenter.Wrap(TNSNotificationCenter.OCClass.defaultCenter).removeObserver(FListener.GetObjectID);
  69. FListener.Free;
  70. end;
  71.  
  72. end.

附件大小
ScreenShotListener.zip10.72 KB