0

我在 ios 中创建了一个文本文件,它存储在以下位置的数据文件中。Sandbox => Documents => mylogfile.txt.

仅当我将设备与 Mac 和 Xcode 连接时,此文件才会更新。如果我弹出设备,然后再次连接以查看更新的文本文件。它没有更新。它将与旧文件相同。与 PC 断开连接时,日志文件不会更新。如何随时更新文本文件。

我的代码:

-(void)logme:(NSString *)mymessage
{
    NSString *content = mymessage;

    //Get the file path
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"locationlog.txt"];

    //create file if it doesn't exist
    if(![[NSFileManager defaultManager] fileExistsAtPath:fileName])
        [[NSFileManager defaultManager] createFileAtPath:fileName contents:nil attributes:nil];

    //append text to file (you'll probably want to add a newline every write)
    NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath:fileName];
    [file seekToEndOfFile];
    [file writeData:[content dataUsingEncoding:NSUTF8StringEncoding]];
    [file closeFile];
}

并将此日志文件称为,

[self logme:myHTML];
4

0 回答 0