0

我想实现一个 iOs 应用程序,它根据某些位置存储(文本)数据。我的问题是,如果我的应用程序被 iOS 杀死,所有数据都会丢失。现在我正在使用文档目录来存储我的 txt 文件。只要应用程序还活着就没有问题......我的问题是我应该在哪里存储我的txt文件以保持它即使应用程序被终止?

4

3 回答 3

1

地图?

“杀死”是指“停止,不再运行”还是“从 iOS 设备中删除”?Priyatham51 的回复反映了您的应用程序运行时间更长,但仍在 iOS 设备上(用户可以再次启动它)。如果您希望数据在从设备中删除的应用程序中保留下来,您可能需要考虑将 NSString 存储到 iCloud 中(您可以从https://developer.apple.com/icloud/index.php之类的内容开始)。

巴特简。

于 2013-11-01T17:20:52.753 回答
0

尝试以下将字符串写入文本文件的代码。我也使用过 NSDocumentoryDirectory。如果您在写入文本文件时仍然发现问题,请告诉我。

NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"Documents directory %@",documentsDirectory);
NSString *fileName = [NSString stringWithFormat:@"%@/Test.txt",
                      documentsDirectory];
NSString *content=@"Hello World";
[content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
于 2013-11-01T17:26:22.133 回答
0

即使在您的应用程序被终止后,保存在 Document 目录中的数据也应该是有效的。您可能在代码中做错了什么。

来自 Apple 文档

Application_Home>/Documents/ 使用此目录来存储关键的用户文档和应用程序数据文件。关键数据是您的应用程序无法重新创建的任何数据,例如用户生成的内容。该目录的内容可以通过文件共享提供给用户。此目录的内容由 iTunes 备份。

https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

于 2013-11-01T17:10:25.013 回答