创建文档文件夹 iPhone SDK

发布于 2024-12-04 17:31:33 字数 291 浏览 0 评论 0原文

我正在开发一个应用程序,当按下正确的按钮时,它会显示几页文本。它是静态专有信息。六个按钮中的每一个都有一个不同的文本文件。

我是 ios SDK 的新手。在 XCODE 中创建项目会自动创建文档文件夹吗? “文档文件夹”就是苹果所说的“沙盒”吗?

我可以简单地编写我的文本(将在屏幕上显示的部分,大量文本),将其放入“文档文件夹”中,然后在按下某个按钮时在 iPhone 上以“滚动模式”显示它吗?

我希望文本成为编译的一部分,因为如果有一种方法可以有效地存储和显示大型文本文件,那么该信息是专有的,而不仅仅是文本文件。

I am developing an App that displays several pages of Text when the correct buttons are pressed. It is Static proprietary information. There is a different Text File for each of six buttons.

I am new to the ios SDK. Does creating a project in XCODE automatically create a Documents Folder? Is the "Documents Folder" what Apple is calling the "Sandbox"?

Can I simply write my Text, (that part which will display on the screen, LOTS of Text), drop it into the "Documents Folder", then display it in "scrolling mode" on the iPhone when a certain button is pressed?

I would prefer the Text to be part of the compile, since the information is proprietary, not simply a Text File, if there is a way to store and display large Text Files efficiently.

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

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

发布评论

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

评论(3

遇到 2024-12-11 17:31:33

是的,Ken,当您创建应用程序时,默认情况下文档目录就在那里,是的,如果您愿意,您当然可以从中写入和读取文本数据。

您不能直接将数据放入文档文件夹中,但是,您需要以编程方式执行此操作。

假设您的文件之一是“TextFile1.txt”。您应该首先将此文件添加到您的项目中,并在 appDelegate 的某处编写以下代码;

NSString *fileBundlePath = [[NSBundle mainBundle] pathForResource:@"TextFile1" ofType:@"txt"];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileDocumentDirectorySavePath = [documentsDirectory stringByAppendingPathComponent:@"TextFile1.txt"];

NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:fileDocumentDirectorySavePath])
    [fm copyItemAtPath:fileBundlePath toPath:fileDocumentDirectorySavePath error:nil];

这会将 TextFile1.txt 复制到您的应用程序文档文件夹中,您可以随时使用以下代码从中读取它;

// You can get the fileDocumentDirectorySavePath same way as in the above code
NSString *stringToDisplay = [[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileDocumentDirectorySavePath] encoding:NSUTF8StringEncoding];
NSLog(@"String : %@", stringToDisplay);

您可以对需要处理的任意数量的文本文件执行此操作。

Yes Ken, the documents directory is there by default when you create an app, and yes, you can certainly write and read text data from it if you want to.

You cannot directly drop data into the Documents folder however, you need to do so programmatically.

Assume that one of your files are 'TextFile1.txt'. You should add this file to your project firstly, and somewhere in the appDelegate, write the following code;

NSString *fileBundlePath = [[NSBundle mainBundle] pathForResource:@"TextFile1" ofType:@"txt"];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileDocumentDirectorySavePath = [documentsDirectory stringByAppendingPathComponent:@"TextFile1.txt"];

NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:fileDocumentDirectorySavePath])
    [fm copyItemAtPath:fileBundlePath toPath:fileDocumentDirectorySavePath error:nil];

This will copy the TextFile1.txt into your apps Documents folder from which you can read it anytime you need with the following code;

// You can get the fileDocumentDirectorySavePath same way as in the above code
NSString *stringToDisplay = [[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileDocumentDirectorySavePath] encoding:NSUTF8StringEncoding];
NSLog(@"String : %@", stringToDisplay);

You can do this for any number of text files you need to work with.

不及他 2024-12-11 17:31:33

如果您不想动态更改文本(即仅在提交更新时),只需将文件直接添加到您的 Xcode 项目中,甚至不用担心沙箱/Documents 文件夹。您可以将文件拖到 Xcode 的侧边栏中(创建一个自定义文件夹对您来说会很有组织性)并选中“将文件复制到项目文件夹吗?”当被问到时。如前所述,它们现在已被复制并成为已编译应用程序的一部分。然后,您可以查询文件并将其显示在 UITextView 中,自动支持文本滚动。

或者,您可以采用我认为更简单的方法,将文件直接包含在代码中。在加载文本的类文件的 .h 文件(标头)中,添加 UITextView 作为属性和变量。在 .m 文件(实现)中,执行 yourTextView = [[UITextView alloc] init];,然后将 yourTextView.text 设置为包含您的文本的 NSString。听起来很混乱,但最终更新会更快更容易。也就是说,除非您的文本已格式化...无论如何,您也可以在 XIB/NIB 文件中创建一个 UITextView 并直接添加文本。

我建议你用代码来做。这将是最容易改变的。

If you don't want to change the text dynamically (i.e., only when you submit an update), just add the files directly to your Xcode project and don't even worry about the sandbox/Documents folder. You can drag the files into the sidebar in Xcode (creating a custom folder for it would be very organized of you) and check the "Copy files to project folder?" when asked. As is stated, they're now copied and part of the compiled app. Then you can query the files and display them in a UITextView, automatically supporting scrolling of text.

Alternatively, you could do what I think is the easier method and include the files directly in your code. In a class file that loads the text, in the .h file (Header), add a UITextView as a property and a variable. In the .m file (Implementation), do yourTextView = [[UITextView alloc] init];, then set yourTextView.text to an NSString containing your text. Sounds confusing, but it will be quicker and easier to update in the end. That is, unless your text is formatted... Anyway, you could also just create a UITextView in your XIB/NIB file and add your text directly.

I'd suggest you do it in code. That will be the easiest to change.

笑梦风尘 2024-12-11 17:31:33

向应用程序添加文本是一回事,确保其安全则更加困难。我必须处理类似的问题,并决定使用未格式化的文本,我将其加密包含在我的应用程序中,并且仅解密正在显示的部分。真正取决于您想对文本保密的程度。请记住,任何人都可以阅读它,并通过屏幕截图直接从屏幕上复制它。此外,使用十六进制编辑器可以轻松读取和提取应用程序中的未加密文本!

或者,您可以准备 *.txt(未格式化)或 html(根据您喜欢的格式)文件格式的文本,并将其包含在您的应用程序中。然而,对于其他人来说,这是复制文件的简单方法。

Adding text to your app is one thing - making it secure is more difficult. I had to deal with a similar issue and decided to use unformatted text that I include encrypted in my app and only decrpt the part that is being shown. Really depends how "secret" you want to keep the text. Remember, anyone can read it anyway and copy it right from the screen with a screenshot. Also unencrypted text in apps can be read and extracted quite easily using a HexEditor!

Alternatively you can prepare the text in *.txt (unformatted) or html (formatted as you like) file format and just include it in your app. However, this is the easies way for others to just copy the file.

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