2
votes

Apple says to place files that store the application state in the "Application Support" folder and to "use the Application Support directory constant NSApplicationSupportDirectory"

https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW11

Delphi's class methods for TPath have all kinds of TPath.GetXXXX (including TPath.GetLibraryPath but I cannot find one that returns the Application support folder.

How can I get the Application Support folder in a Firemonkey app?

1
Does this help you? docwiki.embarcadero.com/Libraries/Tokyo/en/… . Also if you need to store user settings look at docwiki.embarcadero.com/Libraries/Tokyo/en/…Alberto Miola
@AlbertoMiola Not sure what that has to do with OP's question, the Documents folder is nowhere close to the Application Support folder.Jerry Dodge

1 Answers

2
votes

Hard-coded approach (which you probably have tried); since the Application Support folder is under the Library folder:

uses System.IOUtils;

function GetApplicationSupportDir : string;

begin
   Result := TPath.Combine(TPath.GetLibraryPath,'Application Support');
end;

Or, retrieving it directly from iOS or OSX: (For Delphi XE8 and higher)

uses
     Macapi.Helpers,
     {$IFDEF iOS}
     iOSapi.Foundation,
     {$ENDIF}
     {$IFDEF OSX}
     Macapi.Foundation,
     {$ENDIF}

     System.IOUtils;

function GetApplicationSupportDir : string;

var
   Paths : NSArray;
   Dir : NSString;

begin
   // For "Application Support" under the User's Library directory:
   Paths := TNSArray.Wrap(NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, True));

   // For "Application Support" under the System Library directory:
   //Paths := TNSArray.Wrap(NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSSystemDomainMask, True));


   Dir := TNSString.Wrap(Paths.objectAtIndex(0));

   Result := NSStrToStr(Dir);
end;

For XE7, this may work:

uses
     Macapi.Helpers,
     {$IFDEF iOS}
     iOSapi.Foundation,
     {$ENDIF}
     {$IFDEF OSX}
     Macapi.Foundation,
     {$ENDIF}

     System.IOUtils;

///////////////////////////// Added since XE7:

const
   _PU = '_';
   libFoundation = '/System/Library/Frameworks/Foundation.framework/Foundation';

type
   NSUInteger = LongWord;
   NSSearchPathDirectory = NSUInteger;
   NSSearchPathDomainMask = NSUInteger;

function NSSearchPathForDirectoriesInDomains(directory: NSSearchPathDirectory; domainMask: NSSearchPathDomainMask;
  expandTilde: Boolean): Pointer {NSArray}; cdecl;
  external libFoundation name _PU + 'NSSearchPathForDirectoriesInDomains';

/////////////////////////////

function GetApplicationSupportDir : string;

var
   Paths : NSArray;
   Dir : NSString;

begin
   // For "Application Support" under the User's Library directory:
   Paths := TNSArray.Wrap(NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, True));

   // For "Application Support" under the System Library directory:
   //Paths := TNSArray.Wrap(NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSSystemDomainMask, True));

   Dir := TNSString.Wrap(Paths.objectAtIndex(0));

   Result := NSStrToStr(Dir);
end;