我正在使用UIWebview
。我可以使用html文件加载UIWebView
,因为在我的项目中有一些需要,我想从UIWebView
读取完整的数据。我希望在另一个UIWebView
中显示。但我需要的是我想将html数据保存在一个本地文件夹中,我读了alredy。这个保存数据我想加载到第二个webview.this是加载webview和读取webview内容的代码。如何保存在本地文件夹并从该文件夹中恢复。任何人都可以帮助我
将本地html文件加载到webview中:
[webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"b_PctureCardHelp" ofType:@"html"]]]];
阅读webview内容
NSString *yourHTMLSourceCodeString = [webView1 stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];
[webView2 loadHTMLString:yourHTMLSourceCodeString baseURL:nil];
答案 0 :(得分:0)
要将HTML保存在本地文档文件夹中,请使用此
- (void)SaveInfo :(NSString *)HTMLString
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"htmlName.html"];
NSString *helloStr = HTMLString;
NSError *error;
[helloStr writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
}
获取路径并加载
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"htmlName.html"];
NSString* htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[documentsWebView loadHTMLString:htmlString baseURL:nil];
答案 1 :(得分:0)
将您的Html字符串文件保存到app / Library / Cache
NSString *str_html = @"html string";
//yourHTMLSourceCodeString String
NSString *str_lib_path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//Get Library Path
NSString *str_cache_path = [str_lib_path stringByAppendingPathComponent:@"Caches"];
//Get Caches Path
NSString *str_file_path = [str_cache_path stringByAppendingPathComponent:@"file.html"];
//yourHTMLSourceCodeString File Path
[str_html writeToFile:str_file_path atomically:YES encoding:NSUTF8StringEncoding error:nil];
//Save your html string to file path
//then you should find the file in /var/mobile/Applications/(your app uuid)/Library/Caches/file.html
加载Html字符串文件
NSString *str_lib_path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *str_cache_path = [str_lib_path stringByAppendingPathComponent:@"Caches"];
NSString *str_file_path = [str_cache_path stringByAppendingPathComponent:@"file.html"];
//your html string file path
NSString *str_html = [NSString stringWithContentsOfFile:str_file_path encoding:NSUTF8StringEncoding error:nil]
//Get your html file from local file
检查文件路径
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:str_cache_path]) //check caches folder is exist or not
{
[fm createDirectoryAtPath:str_cache_path withIntermediateDirectories:NO attributes:nil error:nil]
//create caches folder
}
//you can also use this method to check the html file is exist or not
祝愿帮助〜