我想把我自己的日志写到iPhone上的一个文本文件中。我写了一个将字符串写入文件的快速方法。现在它将其保存到Documents目录中,如果在设备上打开它会很痛苦,因为我不能只是浏览它。有没有更好的方法可以在我写完这个文件后快速地把它从设备上移走?
/**
* Logs a string to file
*
* @version $Revision: 0.1
*/
+ (void)logWithString:(NSString *)string {
// Create the file
NSError *error;
// Directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"log.txt"];
// Get the file contents
NSData *localData = [NSData dataWithContentsOfFile:filePath];
if (localData) {
NSString *logString = [[NSString alloc] initWithData:localData encoding:NSUTF8StringEncoding];
string = [logString stringByAppendingFormat:@"%@\n", string];
[logString release];
}
// Write to the file
[string writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
}//end发布于 2011-11-01 08:34:48
在Xcode中将Application supports iTunes file sharing添加到应用程序目标的构建信息中:

然后,您可以在Devices > Your device > Apps > File Sharing下的iTunes中轻松浏览、检索和删除应用程序创建的任何文件

发布于 2011-11-01 08:24:08
您可能需要捕获到目前为止已创建的日志数量,并基于此为每个日志创建一个新名称。
因此,您可以将上一个生成的日志名称作为字符串保存在NSUserDefaults中,并从该字符串的末尾获取数字,然后在捕获的整数上添加一个值,为下一个名称做好准备。
因此,如果您有@"Log4“,您可以将4取出来并将其设为5,这样下一个日志就被命名为"Log5”
我的2分:P
发布于 2011-11-01 08:24:52
关于问题中的“如何获取文件”部分
iExplorer,以前的iPhone资源管理器,允许你查看你的应用程序,包括他们的文档文件夹,而不会越狱你的设备。
根据我的经验(尽管是较旧的版本),从手机获取文件可能有点临时(例如,我将一个文件拖到我的桌面上,虽然它创建了文件,但它不写入任何数据),你可以从设备上获取文件。
https://stackoverflow.com/questions/7961180
复制相似问题