0

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
  • The HTML file you want to read, needs to reside within you application sandbox. Refer above link to do that. It is not possible to read HTML file in some other application's bundle without jailbreak. Commented Sep 10, 2013 at 10:24

1 Answer 1

6

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]; 
Sign up to request clarification or add additional context in comments.

8 Comments

Hey, thanks for your answer but sorry to say as i have provided the path of html file at my question is not from the folder from xcode. as i have created one html file and i have saved that file on my local storage and after saving that html file i want that file to show on uiwebview. that path is surly from iphone internal storage or from iphone simulator internal storage. kindly help me if you give any answer to load html file from that iphone internal storage file.
there are two ways you can read your local html file one is if your html file is inside your app and with xcode and other is your html file is at documents directory of your app...so if you need any help in these 2 case i can help you.
hey eptdeveloper , i have updated my question for your batter understanding of my requirement could you please help me ?
from this way you can find your file stored at documents directory and then use it to open it on webview stackoverflow.com/a/6546616/2695503
hey eptdeveloper, thank you very much for your answer , now i am read the file from that document dir path , now my new question is ,now i have read the html file which i have created , how i can load that file on UIWebView programatically ? could you please help me by what code i can load that file path ? my html file path is iphone path : /var/mobile/Applications/FEE7BD45-5714-4F26-A527-A1FBF2871644/Documents/filename.html
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.