【问题标题】:Copy data file from Xcode to iPad sandbox将数据文件从 Xcode 复制到 iPad 沙箱
【发布时间】:2013-03-28 10:55:45
【问题描述】:

我在 Xcode 中创建了一个应用程序,并添加了一个文本文件供该应用程序打开和显示。

如何将此文本文件放入 iPad 沙箱文档目录中进行测试?

【问题讨论】:

    标签: xcode ios5 testing ios-simulator


    【解决方案1】:

    你需要做一些事情:

    1. 创建 Documents 目录的路径
    2. 确保您的文本文件在您的应用程序包中(拖放到 Xcode 中应该没问题)
    3. 将该文本文件按名称从应用程序包复制到 Documents 目录

    试试这个:

    // Documents path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    
    // Destination path
    NSString *fileInDocumentsPath = [documentsPath stringByAppendingPathComponent:@"name-of-your-file.txt"];
    
    // Origin path
    NSString *fileInBundlePath = [[NSBundle mainBundle] pathForResource:@"name-of-your-file" ofType:@"txt"];
    
    // File manager for copying
    NSError *error = nil;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager copyItemAtPath:fileInBundlePath toPath:fileInDocumentsPath error:&error];
    

    【讨论】:

    • 这正是我所需要的。谢谢
    • @Ron,将问题标记为已回答的方法是单击绿色复选标记轮廓。向上/向下箭头用于不同的功能。
    猜你喜欢
    • 2014-05-22
    • 2018-05-05
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    相关资源
    最近更新 更多