【问题标题】:How can I put files in specific directories when building my app?构建应用程序时如何将文件放在特定目录中?
【发布时间】:2012-07-16 19:07:33
【问题描述】:

我已经阅读了很多关于在 iPhone 应用程序中访问文件的信息,但我一开始不知道如何将它们放到那里。我有许多对我的应用程序很重要的声音文件,还有更多可以使用 IAP 购买的文件。我知道这些文件应该放在我的 /Library/Application Support/ 目录中,但是我不知道如何将它们放在那里。我该怎么做呢?我确信我遗漏了一些非常明显的东西,但我无法弄清楚。此外,在我的应用程序中有一个 UITableView 可以跟踪这些文件。当用户购买新声音时,使该表的数据源保持最新的最佳方法是什么?我认为在我的模型中保留一个数组是最好的,但我不确定如何将数组的内容读取和写入磁盘。任何帮助表示赞赏。

【问题讨论】:

    标签: iphone ios file-management


    【解决方案1】:

    您可以使用此代码创建您的文件夹,然后将文件保存在那里。

    NSArray *filePaths =    NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask,YES); 
    NSString *path=[filePaths objectAtIndex: 0];
    NSString *tempPath=[[NSString alloc]initWithFormat:@"%@/%@",path,@"TempFolder"];
    
    BOOL bDir;
    NSError *error;
    if([[NSFileManager defaultManager] fileExistsAtPath:tempPath isDirectory:&bDir]==FALSE){
        [[NSFileManager defaultManager] createDirectoryAtPath:tempPath withIntermediateDirectories:YES attributes:nil error:&error];
    }
    

    然后使用此代码将文件保存在您的文件夹中

    NSArray *filePaths =    NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *pathAndName=[[NSString alloc] initWithFormat:@"%@/TempFolder/%@",
                           [filePaths objectAtIndex: 0],
                           fileName];
    NSData * data = //Load your file here
    [data writeToFile:pathAndName atomically:YES];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-27
      • 2017-10-14
      • 2015-10-23
      • 2015-03-18
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多