无法将文件从主包复制到 iPad 上的文档目录
我从这里的另一篇文章中找到了这段代码。它在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
如果无法访问实际文件列表,您可能需要检查文件名的大小写。 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.
正如我在原始问题中的评论:
我发现有 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.