繁体   English   中英

无法将文件从主包复制到 iPad 上的文档目录

[英]Can't copy files from main bundle to documents directory on iPad

我从这里的另一个帖子中找到了这段代码。 它在 iPad 模拟器模式下按预期工作,但当我切换到实际的 iPad 设备模式时却没有。 相反,它提出了“不存在此类文件”错误消息。 在执行此代码之前,我确实通过右键单击 xxxxxx.app 并选择“显示 package 内容”创建了 Populator 文件夹,并将一些文件放入 Populator 文件夹中。 Xcode 是否将整个应用程序 package 复制到 iPad 设备上? 有什么建议么?

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Populator"];
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"Files"];
NSLog(@"Source Path: %@\n Documents Path: %@ \n Folder Path: %@", sourcePath, documentsDirectory, folderPath);
NSLog(@"docdire: %@", documentsDirectory);
NSLog(@"loadImage button clicked");
label.text = [@"sourcePath: " stringByAppendingString:sourcePath];
textField.text = [@"clicked: " stringByAppendingString:documentsDirectory];;

NSError *error;
if([[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:folderPath error:&error]){
NSLog(@"File successfully copied");
        label.text = @"copy succeeded";
    } else {
        NSLog(@"Error description-%@ \n", [error localizedDescription]);
        NSLog(@"Error reason-%@", [error localizedFailureReason]);
        label.text = [@"Error description: " stringByAppendingString:[error localizedDescription]];
        label2.text = [@"Error reason: " stringByAppendingString:[error localizedFailureReason]];
    }

在 NSBundle 构建后,您不能将文件或文件夹添加到它。 对于设备,Xcode 将签署 NSBundle。 无论您想要在设备上的 NSBundle 中使用什么文件和文件夹,都必须将其添加到您的 Xcode 项目中。

将文件/文件夹添加到 NSBundle 的另一种方法是在捆绑包签名之前的构建阶段。

如果无法访问实际文件列表,您可能需要检查文件名的大小写。 iPhone 模拟器不区分大小写,而 iPad 文件系统区分大小写。 这在过去曾使许多开发人员绊倒。

正如我在原始问题中的评论:

我发现有 2 个地方有 xxxxx.app package。 一个用于模拟器,另一个用于构建分发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM