简体   繁体   中英

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

I am working with UIWebview . I am able to load UIWebView with html file and because of some need in my project i want to read complete data from UIWebView . and I want to display in another UIWebView . but my need is I want to save the html data in one local folder what I read alredy. and this save data I want to load in second webview.this is code to load webview and read webview content.How to save in localfolder and retrive from that folder. Can any one help me

get local html file load into the webview:

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

Read webview Content

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

[webView2 loadHTMLString:yourHTMLSourceCodeString baseURL:nil];

To Save HTML in local documents folder use this

- (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];
}   

For getting the path and loading

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

Save Your Html Stirng File to 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

Load Html String File

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

Check File Path

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

wish help ~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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