简体   繁体   English

我可以在 Xcode 中创建一个 Documents 目录,并通过 UI 将我的 iOS 应用程序的文件放入其中吗? 如果是这样,如何?

[英]Can I create a Documents directory in Xcode, and put files in it for my iOS app through the UI. If so, how?

I need to read/write a tiny amount of info for data persistence in an iOS app.我需要在 iOS 应用程序中读取/写入少量信息以实现数据持久性。 I am using plists and understand that I can read, but not write to the bundle.我正在使用 plists 并且知道我可以读取,但不能写入包。 If I create the plist files initially at development time, how can I place the plists in the "Documents" area of my app so that they are later writable?如果我最初在开发时创建 plist 文件,如何将 plist 放置在我的应用程序的“文档”区域中,以便以后可写?

Short answer: No.简短的回答:没有。

As you say, an iOS app's bundle is not writable, but it is readable.正如您所说,iOS 应用程序的包不可写,但可以读取。

Further, an app's documents directory doesn't exist until the app is run (on the user's target device) for the first time.此外,在应用程序首次运行(在用户的目标设备上)之前,应用程序的文档目录不存在。 Thus, you can't build your app's documents directory at compile-time, nor can you submit an app to the app store with a prepopulated Documents directory.因此,您无法在编译时构建应用程序的文档目录,也无法使用预填充的文档目录将应用程序提交到应用程序商店。 (It would be nice if Apple gave us the ability to provide a "seed" documents directory that got moved to the documents directory at first launch, but they do not provide that facility -- at least not that I'm aware of.) (如果 Apple 让我们能够提供一个“种子”文档目录,该目录在第一次启动时移到文档目录中,那就太好了,但他们没有提供这种功能——至少我不知道。)

What you can do is to create a sub-directory of your bundle that contains a set of files that you want to place in the app's documents directory.可以做的是创建包的子目录,其中包含要放置在应用程序文档目录中的一组文件。

On launch, check a "hasBeenInstalled" flag in UserDefaults .上启动,检查“hasBeenInstalled”标志UserDefaults If it's true, do nothing.如果是真的,什么都不做。 if it's false, use the file manager to copy files from your app's bundle into the app's documents directory, then write hasBeenInstalled=true to UserDefaults .如果为 false,请使用文件管理器将文件从您的应用程序包复制到应用程序的文档目录中,然后将hasBeenInstalled=true写入UserDefaults

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

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