在Delphi中获取MacOS应用程序支持文件夹 [英] Get the MacOS Application Support folder in Delphi
问题描述
Apple表示将存储应用程序状态的文件放置在应用程序支持"文件夹中,并使用应用程序支持目录常量NSApplicationSupportDirectory"
Apple says to place files that store the application state in the "Application Support" folder and to "use the Application Support directory constant NSApplicationSupportDirectory"
Delphi的TPath类方法具有各种TPath.GetXXXX(包括TPath.GetLibraryPath,但是我找不到返回Application Support文件夹的类.
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.
如何在Firemonkey应用程序中获取应用程序支持"文件夹?
How can I get the Application Support folder in a Firemonkey app?
推荐答案
硬编码方法(您可能已经尝试过);因为应用程序支持"文件夹位于库"文件夹下:
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;
或者,直接从iOS或OSX检索它(对于Delphi XE8及更高版本)
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;
对于XE7,这可能有效:
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;
这篇关于在Delphi中获取MacOS应用程序支持文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!