1

我想在我的 iPhone 应用程序中保存一个 .txt 文件,但我希望它能够持续存在,即使应用程序被卸载并重新安装,我仍然可以访问它。

我曾尝试将文件保存在 iPhone 的硬盘上,但始终无法正常工作。

这适用于模拟器:

NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];


//NSString *dataFile= @"/Users/rajesh/Library/Application Support/iPhone Simulator/4.2/tmp/myfile.dat"; //on simulator
NSData *databuffer;
databuffer = [textBox.text dataUsingEncoding: NSASCIIStringEncoding];
[filemgr createFileAtPath: dataFile contents: databuffer attributes:nil];
[filemgr release];

我在设备上使用它:

NSString *dataFile= @"/var/mobile/Applications/temp/myfile.dat"; //on device

但它不起作用。

4

2 回答 2

1

没有办法这样做,因为所有文件都在沙箱中,它是您应用程序的一部分,您不能在沙箱之外写入文件。

您可以尝试将文件保存在钥匙串中(肮脏的黑客)或通过 iCloud 存储。

于 2011-11-08T09:18:17.867 回答
0

写入共享文件夹无法支持您的用例,因为 iOS 中没有这样的功能。但是,您的高级目标(即使文件已被删除并重新安装后仍可供应用程序使用)可以使用 iCloud 提供服务,因此请查看通过 iCloud 同步文件。

于 2011-11-08T09:17:38.883 回答