Skip to content

Instantly share code, notes, and snippets.

@DelphiWorlds
Last active March 16, 2021 13:22
Show Gist options
  • Save DelphiWorlds/7b1c2d32237688e6b491a0d9b1a4d16b to your computer and use it in GitHub Desktop.
Save DelphiWorlds/7b1c2d32237688e6b491a0d9b1a4d16b to your computer and use it in GitHub Desktop.
Sending a file or directory to the trash on macOS/iOS. NOTE: *** On iOS, works only on iOS 11 or above ***
uses
{$IF Defined(IOS)}
Macapi.ObjectiveC,
iOSapi.Foundation,
{$ELSE}
Macapi.Foundation,
{$ENDIF}
Macapi.Helpers;
{$IF Defined(IOS)}
type
NSFileManager = interface(iOSapi.Foundation.NSFileManager)
['{238C346D-D21F-4CC7-9966-125C68F46259}']
function trashItemAtURL(url: NSURL; resultingItemURL: NSURL; error: PPointer = nil): Boolean; cdecl;
end;
TNSFileManager = class(TOCGenericImport<NSFileManagerClass, NSFileManager>) end;
{$ENDIF}
function FileManager: NSFileManager;
begin
Result := TNSFileManager.Wrap(TNSFileManager.OCClass.defaultManager);
end;
function TrashItem(const AItem: string): Boolean;
var
LError: Pointer;
begin
// LError needs to be initialized to nil, because if trashItemAtURL succeeds, it does not touch the reference
LError := nil;
FileManager.trashItemAtURL(TNSURL.Wrap(TNSURL.OCClass.fileURLWithPath(StrToNSStr(AItem), True)), nil, @LError);
Result := LError = nil;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment