【问题标题】:Best location to store config, plugins for my OS X application存储配置的最佳位置,我的 OS X 应用程序的插件
【发布时间】:2014-11-07 19:02:55
【问题描述】:

我正在编写一个 OS X 应用程序,并且想知道将应用程序数据(如我的程序的配置文件和插件)存储到的最佳位置。

配置不是默认格式,因为我也在 Windows 上部署了这个应用程序。

【问题讨论】:

    标签: macos desktop-application


    【解决方案1】:

    通常在应用程序支持目录内的应用程序子文件夹中,预计会存储诸如此类的内容。 Apple 在其文档中提供了一个很好的功能,可以为您的应用程序支持目录获取标准化的NSURL

    摘自their documentation:

    - (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 中的子路径,但他们建议使用您的包标识符,如上例所示。

    【讨论】:

      猜你喜欢
      • 2014-11-20
      • 2010-09-10
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 2020-08-22
      • 2011-12-07
      • 2017-06-19
      • 2013-06-02
      相关资源
      最近更新 更多