iOS UIWebview Load local device HTML file path [du

2019-09-14 21:20发布

问题:

This question already has an answer here:

  • How to load local html file into UIWebView 17 answers

I am developing one iOS application in which I want to load the HTML file on UIWebView, my html file path is as like below,

iPhone HTML file path:

/var/mobile/Applications/65A8E000-21A6-429E-90A0-6BF03BAB3EA5/SarkBuilderApp.app/SarkBuilerHtmlFile_new.html

I want to load this iPhone device's HTML file path to UIWebView.

How can I do this?


Thank you very much for your quick answer. Let me explain my basic requirement, I am devleoping one iOS application in which my requirement is to fill some field on the screen and generate a .pdf file.

For that I have taken one pre-defined HTML form, and after that I updated that .html file and saved it via the code below:

NSString *fileName = @"new_file_htmlfile.html";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *html_file_name_location = [documentsDirectory stringByAppendingPathComponent:fileName];

NSError *error;

// Write the file
[data writeToFile:html_file_name_location atomically:YES
        encoding:NSUTF8StringEncoding error:&error];

By doing that I have generated the new HTML file and I have read the file path from the code. The iPhone device path is:

/var/mobile/Applications/65A8E000-21A6-429E-90A0-6BF03BAB3EA5/SarkBuilderApp.app/SarkBuilerHtmlFile_new.html

iPhone emulator path is:

/Users/rightwaysolution/Library/Application Support/iPhone Simulator/6.1/Applications/20748991-EE5C-44FE-ACFF-D93542FCF95B/Documents/new_file_htmlfile.html

Now I am trying to load that iPhone device HTML file on my UIWebView so that I can convert it into a .pdf.

Could you please explain to me how I can access that file?

回答1:

use this code to achieve what you want

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"SarkBuilerHtmlFile_new" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];

but for this make sure your html file must be within your app folder in xcode

Edits:-

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"txtFile.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

NSString* htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];

OR

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"txtFile.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

NSString* htmlString = [NSString stringWithContentsOfFile:content encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];