xcode 如何在iphone构建阶段将文件复制到库或文档目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1382312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to copy files to Library or Document dir in iphone build phase?
提问by fallhunter
I want copy some files to Library
or Document
in the Product directory,
but when I add a build phase and select Products Directoryin destinationand inputmy subdir it's like Library/xxx/
.
我想一些文件复制到Library
或Document
在产品目录中,但是当我添加构建阶段,选择产品目录中的目的地和输入我的子目录滋味Library/xxx/
。
When I run the App in the simulator, I found nothing in the destinationand if I set the destinationto Resources
, it will be there.
当我在模拟器中运行应用程序时,我在目的地中找不到任何东西,如果我将目的地设置为Resources
,它就会在那里。
Can you please tell me why this is happening and how I can make it?
你能告诉我为什么会发生这种情况以及我如何做到吗?
回答by Marcus S. Zarra
The documents directory should only be accessed at runtime. To my knowledge you cannot copy items there during building. What you should do is store those objects that you want in the Documents directory within your bundle and then copy them upon first launch.
文档目录只能在运行时访问。据我所知,您不能在构建过程中复制项目。你应该做的是将你想要的那些对象存储在你的包中的 Documents 目录中,然后在第一次启动时复制它们。
回答by Hunter
Interesting question. I don't know if you can do this as part of the build phase.
有趣的问题。我不知道您是否可以在构建阶段执行此操作。
To get something into the Documents
directory, I have a method that runs at app startup like this:
为了将某些内容放入Documents
目录,我有一个在应用程序启动时运行的方法,如下所示:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"filename.txt"];
BOOL success = [fileManager copyItemAtPath:defaultDBPath writablePath error:&error];
That's an incomplete fragment but you probably get the idea. The important thing is the NSDocumentDirectory
constant.
这是一个不完整的片段,但您可能已经明白了。重要的是NSDocumentDirectory
常数。
回答by CiNN
Add a new build phase -> New Copy Files Build Phase in Target
在目标中添加一个新的构建阶段 -> 新建复制文件构建阶段