Last active
March 16, 2021 13:22
-
-
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 ***
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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