【问题标题】:Create temp file for a sandboxed Cocoa application为沙盒 Cocoa 应用程序创建临时文件
【发布时间】:2012-09-11 10:16:55
【问题描述】:

我的应用是沙盒(根据最新的 App Store 指南),我想创建一些临时文件。

我可以这样做吗?如果“是”,我在哪里可以这样做?有没有预先指定的路径? (以及访问该路径的命令?)

【问题讨论】:

  • 请将您的解决方案作为答案发布,而不是将其添加到问题正文中。

标签: objective-c cocoa app-store sandbox temporary-files


【解决方案1】:

您应该使用NSTemporaryDirectory() 函数,该函数将为您的应用程序找到并返回适当的临时文件夹(无论沙盒状态、操作系统版本和许多其他内容如何)。查看this Cocoa With Love 帖子,了解有关NSTemporaryDirectory() 和其他临时目录相关详细信息的更多详细信息。

【讨论】:

    【解决方案2】:

    NSHipster 上有一篇关于临时目录的好文章:

    http://nshipster.com/nstemporarydirectory/

    作者建议这段代码也可以与沙盒应用完美配合:

    NSError *error;
    NSString *globallyUniqueString = [[NSProcessInfo processInfo] globallyUniqueString];
    NSString *tempDirectoryPath = [NSTemporaryDirectory() stringByAppendingPathComponent:globallyUniqueString];
    NSURL *tempDirectoryURL = [NSURL fileURLWithPath:tempDirectoryPath isDirectory:YES];
    [[NSFileManager defaultManager] createDirectoryAtURL:tempDirectoryURL withIntermediateDirectories:YES attributes:nil error:&error];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-17
      • 1970-01-01
      • 2012-06-22
      相关资源
      最近更新 更多