繁体   English   中英

从uiwebview读取HTML内容并加载到本地文件夹中,并检索以加载另一个Webview

[英]Read Html Content from uiwebview and load into local folder and retrive to load another webview

我正在使用UIWebview 我能够用html文件加载UIWebView ,并且由于我项目中的某些需要,我想从UIWebView读取完整的数据。 我想在另一个UIWebView显示。 但是我需要将html数据保存在一个我已经看过的本地文件夹中。 然后保存要在第二个webview中加载的数据。这是加载webview和读取webview内容的代码。如何保存在localfolder中并从该文件夹中检索。 谁能帮我

将本地html文件加载到webview中:

[webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"b_PctureCardHelp" ofType:@"html"]]]];

阅读网页内容

NSString *yourHTMLSourceCodeString = [webView1 stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];

[webView2 loadHTMLString:yourHTMLSourceCodeString baseURL:nil];

要将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];

将您的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

希望帮助〜

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM