iPhone应用程序可以写出文件吗? [英] Can an iPhone application write out files?

查看:89
本文介绍了iPhone应用程序可以写出文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道iOS应用程序在沙盒中运行,但我想我记得Apple提到了一种方法,可以让应用程序写入和读取公共文件共享,这样您就可以在计算机上处​​理这些文件了。你插入iPhone(安装驱动器)。我认为这是在iPad的背景下,但我不确定。

So I know iOS apps run in a sandbox and all, but I thought I remembered Apple mentioning a way for apps to write to and read from a common file share, so that you can then deal with these files on your computer when you plug you iPhone in (mount the drive). I think this was in the context on the iPad, but I'm not sure.

有谁知道更多这方面的内容?搜索是一件很难的事。

Anybody know anything more of this? It's kind of a tough thing to search for.

推荐答案

每个应用程序都有一个Documents目录,可以在其中编写文件。要获取应用程序文档目录,您可以使用:

Each app has a Documents directory where it can write files. To get the apps Documents directory you can use:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

写下Hello,World!到文件目录中名为 hello.txt 的文件,您可以执行以下操作:

To write say "Hello, World!" to a file called hello.txt in the documents directory you could do something like:

NSString *hello = @"Hello, World!";
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:@"hello.txt"];
[hello writeToFile: myFile atomically:YES encoding:NSUTF8StringEncoding error:NULL];

如果你在你的应用中设置 UIFileSharingEnabled code> Info.plist 到 YES 当设备连接并且用户可以访问时,文件将在iTunes中显示从那里。

If you set UIFileSharingEnabled in your app's Info.plist to YES the the files will be visible in iTunes when the device is connected and can be accessed by the user from there.

这篇关于iPhone应用程序可以写出文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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