如何覆盖或保存在UIWebView中编辑的现有本地HTML文件?

时间:2013-03-25 02:05:16

标签: html ios file uiwebview local

该应用程序使用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];
}

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

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

0 个答案:

没有答案