0

该应用程序使用 UIWebView 加载本地 HTML 文件(现在只是文本)以进行编辑。我需要将用户编辑的内容保存回本地文件。这是我能想到的唯一方法:

用户编辑文本后,我编写了一个保存方法,该方法在 Web 视图中获取当前 HTML,并将其保存到写入文档目录的 HTML 文件(与原始文件名相同)。理论上这应该覆盖原来的。但事实并非如此。

-(void)save{
    NSError *err;
    NSString * docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *oldFile = [self.webView.request.URL lastPathComponent];
    //Above line gets the actual HTML file name that's in the webview
    NSString * path = [docsDir stringByAppendingPathComponent:oldFile];
    //The following line gets what html code is in the actual webview
    NSString *html = [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];

    [html writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

似乎没有写入任何文件。它不会覆盖原始文件,也不会写入新文件。

也许我不应该以这种方式保存它,但我想不出任何其他方法来做到这一点。

4

0 回答 0