如何将文本从iOS应用程序导出到台式机? [英] How do I export text from iOS application to desktop PC?

查看:117
本文介绍了如何将文本从iOS应用程序导出到台式机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我写了一个iOS应用程序,允许用户输入一些文本并保存。如何导出此文本以在运行Windows操作系统的台式机上查看?

Lets say I have written an application for iOS which allows the user to enter some text and save it. How can I export this text to be viewed on a desktop PC running windows OS? If this is not possible, what are the alternatives?

推荐答案

您可以将文本保存到应用程序的Documents目录中,并允许他们通过iTunes导出。

You can save the text to your app's Documents directory and allow them to export it through iTunes. You can also allow them to email it.

将其保存到磁盘

要保存在App的文档目录中,您需要做一些事情。首先,您需要获取到路径目录的URL。当您制作基于Core Data的项目时,可以通过Xcode方便地生成此方法。这里的方法:

To save in the App's documents directory, you'll need to do a few things. First of all, you'll need to get the URL to the path directory. A method for this is conveniently generated by Xcode when you make a Core Data based project. Here's that method:

- (NSURL *)applicationDocumentsDirectory{
  return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

接下来,您需要使用该URL,我们到你的应用程序的文档目录。在simples的情况下,我们有一个字符串, someText ,我们将写入到Documents目录。下面是这个样子:

Next, you'll want to take that that URL and use it to write our to your App's documents directory. In the simples case, we have a string, called someText, which we'll be writing out to the Documents directory. Here's what that looks like:

NSString *someText = "Here's to some awesome text.";

我们有一个路径,名为 path 。注意,我们获取文档目录的路径,然后附加一个文件名。您可以用要使用的任何文件名替换 someText.txt

We have a path, called path. Note that we take the path to the documents directory and then append a filename. You could replace someText.txt with whatever filename you want to use.

NSString *path = [NSString stringWithFormat:@"%@",[[[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"someText.txt"] absoluteString]];

我们告诉字符串自己写入文件(在本例中为atomically)失败,我们填充错误对象,如果需要,我们可以稍后读出。在这里注意原子的选项。如果设置为 YES ,应用程序会将文本写入缓冲区,然后重命名。如果未设置为 YES ,则文本将直接写入文本。这在多线程环境中有所不同,并且可以保护您的文本不受某些损坏的结果影响,但是原子写入速度较慢。

We tell the the string to write itself out to the file (in this case atomically) and if it fails, we populate the error object, which we can read out later if necessary. Note the option for "atomically" here. If it's set to YES, the app will write the text into a buffer and rename it afterward. If it's not set to YES, the text will be written straight to text. This makes a difference in multithreaded environments and can protect your text from being some mangled result, but atomic writing is slower.

以下是上述所有代码:

//Write to the file
[someText writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];

NSString *someText = "Here's to some awesome text.";

NSError *error = nil;

NSString *path = [NSString stringWithFormat:@"%@",[[[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"someText.txt"] absoluteString]];

//Write to the file
[someText writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];

从iTunes读取档案

这是有趣的部分。在Xcode中,您需要向应用程序的Info.plist文件中添加一个键。下面是Xcode 4中的内容:

This is the fun part. In Xcode, you need to add a key to your app's Info.plist file. Here's what that is going to look like in Xcode 4:

现在,在iTunes中,您的应用程序的文档目录将可见。

Now, in iTunes, your App's documents directory will be visible.

或者(或者除iTunes之外),您可以使用MessageUI框架,并允许用户通过电子邮件发送文件。

Alternatively (or in addition to iTunes), you could use the MessageUI framework and allow the user to email the file.

这篇关于如何将文本从iOS应用程序导出到台式机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆