【发布时间】:2014-11-07 19:02:55
【问题描述】:
我正在编写一个 OS X 应用程序,并且想知道将应用程序数据(如我的程序的配置文件和插件)存储到的最佳位置。
配置不是默认格式,因为我也在 Windows 上部署了这个应用程序。
【问题讨论】:
我正在编写一个 OS X 应用程序,并且想知道将应用程序数据(如我的程序的配置文件和插件)存储到的最佳位置。
配置不是默认格式,因为我也在 Windows 上部署了这个应用程序。
【问题讨论】:
通常在应用程序支持目录内的应用程序子文件夹中,预计会存储诸如此类的内容。 Apple 在其文档中提供了一个很好的功能,可以为您的应用程序支持目录获取标准化的NSURL。
- (NSURL*)applicationDirectory
{
NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier];
NSFileManager*fm = [NSFileManager defaultManager];
NSURL* dirPath = nil;
// Find the application support directory in the home directory.
NSArray* appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
if ([appSupportDir count] > 0)
{
// Append the bundle ID to the URL for the
// Application Support directory
dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:bundleID];
// If the directory does not exist, this method creates it.
// This method is only available in OS X v10.7 and iOS 5.0 or later.
NSError* theError = nil;
if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
attributes:nil error:&theError])
{
// Handle the error.
return nil;
}
}
return dirPath;
}
您可以随意调用NSApplicationDirectory 中的子路径,但他们建议使用您的包标识符,如上例所示。
【讨论】: