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

发布于 2024-11-10 11:38:15 字数 1444 浏览 0 评论 0原文

我从这里的另一篇文章中找到了这段代码。它在 iPad 模拟器模式下按预期工作,但当我切换到实际 iPad 设备模式时却无法工作。相反,它出现了“不存在此类文件”的错误消息。在执行此代码之前,我通过右键单击 xxxxxx.app 创建了一个 Populator 文件夹,然后选择“显示包内容”并将一些文件放入 Populator 文件夹中。 Xcode 是否会通过 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]];
    }

I found this piece of code from anther post here. It worked as intended with iPad simulator mode but not when I switched to the actual iPad device mode. Instead, it came up with a "no such files exist" error message. Before executing this codes, I did create a the Populator folder by right-click on the xxxxxx.app and chose "show package contents" and put some files into the Populator folder. Does Xcode copy the whole app package over the iPad device? Any suggestions?

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]];
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静谧 2024-11-17 11:38:15

NSBundle 构建完成后,您无法将文件或文件夹添加到其中。对于设备,Xcode 将签署 NSBundle。无论您想要在设备上的 NSBundle 中包含什么文件和文件夹,都必须将其添加到您的 Xcode 项目中。

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

You cannot add files or folder to the NSBundle after it has been built. For the device, Xcode is going to sign the NSBundle. Whatever files and folders you want in the NSBundle on the device will have to be added to your Xcode project.

The other way to add files/folders to your NSBundle is during the build phase before the bundle is signed.

甚是思念 2024-11-17 11:38:15

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

Without access to the actual file list, you might want to check the case of the file names. The iPhone simulator is not case-sensitive, while the iPad filesystem is Case-Sensitive. This has tripped up many developers in the past.

转身泪倾城 2024-11-17 11:38:15

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

我发现有 2 个地方有 xxxxx.app 包。一种用于模拟器,另一种用于构建发行版。

As in my comment in the original question:

I found out that there are 2 places that has the xxxxx.app package. One is for the simulator and the other one is for the build distribution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文