所以我知道 iOS 应用程序在沙箱中运行,但我想我记得 Apple 提到了一种应用程序写入和读取公共(public)文件共享的方法,以便您可以在计算机上处理这些文件将 iPhone 插入(安装驱动器)。我认为这是在 iPad 上的上下文,但我不确定。
有人知道更多吗?搜索起来有点困难。
最佳答案
每个应用程序都有一个可以写入文件的 Documents 目录。要获取应用程序文档目录,您可以使用:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
要写“Hello, World!”对于文档目录中名为 hello.txt
的文件,您可以执行以下操作:
NSString *hello = @"Hello, World!";
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:@"hello.txt"];
[hello writeToFile: myFile atomically:YES encoding:NSUTF8StringEncoding error:NULL];
如果您将应用的 Info.plist
中的 UIFileSharingEnabled
设置为 YES
,则当设备连接时,文件将在 iTunes 中可见,并且用户可以从那里访问。
关于iphone - iPhone 应用程序可以写出文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3619570/